FAQ Database Discussion Community


UnknownFormat in rails spec

ruby-on-rails-4,rspec-rails
I'm fairly new to rails and I'm trying to return a javascript file from a controller action. In TDD fashion I'm trying to write a failing test first and although I've fixed a bunch of reasons it's failed since starting, I'm now up against an error that I'm not sure...

`require': cannot load such file — spec_helper (LoadError)

ruby,rspec,rspec2,rspec-rails,rspec3
I am creating bundler gem --test=rspec MyGem. in which i m getting reository structure. while i m runing rspec code i m getting error. `require': cannot load such file -- spec_helper (LoadError) I try to apply require relative still i m getting error like [email protected]:~/sheetal/spec$ rspec sheetal_spec.rb \/home/sheetal/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot...

model's class trying to access database even using double

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails
I have a model structure like this models/ -- a.rb -- b.rb -- project_a/ ---- a.rb ---- b.rb the classes inside the folder project_a are classes from another Rails project. I don't have the migrations for them, I just use them to call find. It is a project to migrate...

RSpec Devise Session Testing

ruby-on-rails,ruby,rspec,devise,rspec-rails
With rails 4.2.0 and the latest version of RSpec I generated a controller test. How would I ensure my admin user is logged in? For example: if current_user.admin? In the rspec test it mentions it like so: let(:valid_session) { {} } How would I enter a valid session?...

Rspec can't stub where or find_by_, only find

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,stub
I'm going to share several pairs of run code and test code. Basically, the test code is only working when I use a find on the object class, but the problem is find is the one method I DON'T want to use because I'm not looking for the primary key!...

Rspec/Capybara features tests not running, even with _spec.rb ending

rspec,capybara,rspec-rails
When I run my Rspec feature examples, they are not being detected. When I specify the route to my /features folder, the message I get is " No examples found". I'm not sure if it's a requirement issue or something I'm missing with my test. my feature test: require "rails_helper"...

Rspec view test with url parameters

ruby-on-rails,unit-testing,testing,rspec,rspec-rails
I have a page dashboard.html.erb that may be redirected to from several different controllers/actions. When it receives the redirect, it also receives several url parameters. Controller code: class PlansController def some_action redirect_to dashboard_path(show: "default", cool_array: ["test", "test"]) end end class StaticpagesController def dashboard end end View code: <% if cool_array...

No route matches in Rspec Rails

ruby,ruby-on-rails-4,rspec-rails
I'm using Rails 4.1 and I have an API json, but doesn't work on Rspec-Rails. rspec-rails - Version used 3.2.1 $ rake routes GET /cities/:code(.:format) cities#show cities_controller.rb class CitiesController < ApplicationController respond_to :json def show code = params[:code] respond_with City.find_by_code(code) end end cities_controller_spec.rb require 'rails_helper' RSpec.describe CitiesController, :type => :controller...

Factory still not registered after trying solutions provided

ruby-on-rails,factory-girl,rspec-rails
I am trying to write an Rspec test for the first time, my model test worked out fine, but I have problems with my controller test. Which seems weird. For my model test, I'm following the example from: https://gist.github.com/kyletcarlson/6234923 I am given the infamous Factory not registered: error. The log...

Stubbing tomorrow's date in rspec

rspec-rails,ruby-on-rails-4.1
I have a method in which am sending tomorrow's date(1.date.from_now). I'm testing the same method in my specs. Can anyone tell me how to stub that date. if I pass 1.date.from now. am getting following error. <S3Client (class)> received :upload_csv_by_path with unexpected arguments expected: ("/home/projects/sh/tmp/test_exporter.csv", nil, {:expires=>Sat, 07 Feb 2015...

How to disable a before_action in a controller spec?

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails
I've employed the use of this in my controller spec: controller.class.skip_before_action Specifically, in this case: controller.class.skip_before_action :require_authorisation_to_view_materials MaterialsController: class MaterialsController < ApplicationController before_action :set_material, only: [:show, :edit, :update, :destroy] before_action :require_authorisation_to_view_materials, only: [:index, :show] def require_authorisation_to_view_materials # For Materials Page unless user_signed_in? &&...

Using Rspec should_receive to test that a controller calls a method on an object correctly

ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec-rails
OK first, I should say while I've read a lot about should_receive, I'm still not entirely sure I'm understanding the concept behind it, so what I'm doing could potentially be completely not possible. I have the following: class PlansController def destroy plan = plan.find_by_id(params[:id]) if plan.cancel_stripe_subscription(params[:reason]) flash[:success] = "success" redirect_to...

How do I test the comments of the current_user in RSpec?

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,rspec3
So I have a User that has_many :comments. In my Comments#Index, I have this: def index @comments = current_user.comments end Inside my Rspec.config... block in rails_helper.rb I have this: # Add Config info for Devise config.include Devise::TestHelpers, type: :controller My comments_controller_spec.rb looks like this: describe 'GET #index' do it "populates...

Is there a way not to generate controller specs by default?

ruby-on-rails,rspec-rails
I'm a fan of Rspec, but I never use either controller or view specs. I know the following options exist: Usage: rails generate controller NAME [action action] [options] Rspec options: [--controller-specs], [--no-controller-specs] # Indicates when to generate controller specs # Default: true [--view-specs], [--no-view-specs] # Indicates when to generate view...

RSpec matcher that checks collection to include item that satisfies lambda

ruby,rspec,rspec-rails,rspec3
I am a bit at a loss as to how to write a RSpec 3.2.x spec that checks wether a list contains at least one item that satisfies a condition. Here is an example: model = Invoice.new model.name = 'test' changes = model.changes expect(changes).to include { |x| x.key == 'name'...

Rails RSpec invalid option: --format

ruby-on-rails,rspec,rspec-rails
$ rspec spec --format documentation ... test results invalid option: --format Test::Unit automatic runner. Usage: ... Why rspec warn me about this option? Test results printed exactly in this format "documentation", but with this warning and usage. $ bundle show | grep rspec * rspec-core (3.2.0) * rspec-expectations (3.2.0) *...

Rspec test public controller method passing params to private controller method

ruby-on-rails,ruby-on-rails-4,rspec,controller,rspec-rails
This is my controller: class PlansController def use_new_card @user = User.find_by_id(new_card_params[:user_id]) if new_stripe_card ... end private def new_card_params params.permit(:user_id, :stripeToken) end def new_stripe_card StripeService.new({user_id: @user.id, customer_id: @user.stripe_customer_id, source: new_card_params[:stripeToken]}).new_card end end I'm trying to write a controller spec that tests that when the proper parameters are POST to the use_new_card...

How to select 3rd 'a' tag in a list for RSpec test?

ruby-on-rails,rspec,css-selectors,click,rspec-rails
How do I select a link in a list to click on for an rspec acceptance test. The code for the view is (in html): %ul.exports %li.model .name Model 1 .control link_to 'Export', export_model_path(:model1) I've tried (page.find("a")[:href] = "/admin/export/users").click and the response is 'Capybara Ambiguous match'. I've also tried variations...

RSpec example not working with before_validation

ruby-on-rails,rspec,rspec-rails,shoulda
I have a fairly simple model being saved, but am getting RSpec failures that I cannot find the solution to. I am also using the shoulda gem. CharacterSheet model: class CharacterSheet < ActiveRecord::Base validates :character_name, :player_name, :strength, :strength_modifier, presence: true before_validation :calculate_strength_modifier, on: :create def calculate_strength_modifier self.strength_modifier = ((self.strength -...

Rspec, can you stub a method that doesn't exist on an object (or mock an object that can take any method)?

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails,stub
This might sound like a terribly basic question... so let me explain the context. I'm retrieving an event from Stripe, but for the purposes of this, but this works for other things that I've considered. The data that I need in the Stripe object is way buried, so I'd have...

How to regenerate config/application.rb in Rails 4.2?

ruby-on-rails,rspec-rails,minitest,ruby-on-rails-4.2,rails-generate
Does Rails 4.2 provide a single command that regenerates config/application.rb in an existing application? The reason I ask is imagine a Rails app is rails new-ed without the --skip-test-unit option. Then at a much later date, this app is switched to RSpec. How can config/application.rb be regenerated as if the...

Watir Rspec Ruby : Comparing new/edited value with old/previous value

ruby-on-rails,ruby,rspec,watir,rspec-rails
Auto generated name twice, first $name = generate_name self.name = $name produces: $name is "Abc" And then edited the $name to be "Xyz". Which causes the previous/old name to be no longer available/saved in the app, its overwritten by new/edited name. I now have to compare the two values of...

Validate presence of attributes using RSpec

rspec-rails,rspec3
Some attributes in my model have presence validation and I wanted to add tests in my spec to check if an error is generated when the attribute is blank. I'm using this code: it 'should have a name' do expect(@patient.errors[:name].size).to eq(1) end But here is the result of the rspec...

uninitialized constant Rspec

rspec-rails
I created a new rails application and followed the installation instructions for rspec-rails here - https://github.com/rspec/rspec-rails I then created (copied from the interwebs) the following module in my app/lib directory. require 'openssl' require 'base64' module Cipher def self.encrypt(key, data) data += 'A' # Add 'A' suffix to support empty data...

the scheme redis does not accept registry part: : (or bad hostname?) (URI::InvalidURIError)

ruby-on-rails,ruby-on-rails-4,rspec,rspec-rails
I have installed the Rspec and trying to run the first test for the models but I am getting this error .rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/uri/generic.rb:214:in `initialize': the scheme redis does not accept registry part: : (or bad hostname?) (URI::InvalidURIError) from /Users/ewamarciniak/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/uri/common.rb:218:in `new' before the test starts running. I am using Redis and Sidekick...

`method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block)

ruby-on-rails,unit-testing,testing,rspec,rspec-rails
I wanted to remove the FactoryGirl.build(:user) everytime I want to create a user and so I added these lines: RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end to the spec_helper.rb. But this creates the following error: `method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block). It...