RSpec let method gives nil when used in specs - ruby-on-rails-3

When I run this test
require 'spec_helper'
describe AssignmentsController do
let(:user) { create(:user) }
let(:course) { create(:course) }
describe "GET 'index'" do
it "returns http success" do
assignment = user.assignments.build(name: "Hello 2", start_date: "5/20/2000", due_date: "5/21/2000")
get :index
assigns(:assignment).should eq([assignment])
end
end
end
I get this
Failure:
1) AssignmentsController GET 'index' returns http success
Failure/Error: get :index
NoMethodError:
undefined method `assignments' for nil:NilClass
# ./app/controllers/assignments_controller.rb:5:in `index'
# ./spec/controllers/assignments_controller_spec.rb:29:in `block (3 levels) in <top (required)>'
Why this be since I defined the user variable with lets above

If you look carefully, you'll see that the error originating from your controller on line 5, not the spec file. Everything should be fine with the let statement the way you have it.

Related

Rails API post in request spec should pass params but getting empty

To start, in my request spec sites.spec.rb I have this test:
describe "POST /v1/sites" do
let(:valid_attributes) { { url: "www.example.com", site_code: "123456" } }
context 'when the request is valid' do
before { post v1_sites_path, params: valid_attributes }
it 'creates a site' do
expect(json['url']).to eq("www.example.com")
expect(json['site_code']).to eq("123456")
end
it 'returns status code 201' do
expect(response).to have_http_status(201)
end
end
I then get a failing test for "creates a site"...
1) Sites POST /v1/sites when the request is valid creates a site
Failure/Error: expect(json['url']).to eq("www.example.com")
expected: "www.example.com"
got: ["can't be blank"]
(compared using ==)
# ./spec/requests/sites_spec.rb:61:in `block (4 levels) in <top (required)>'
# ./spec/rails_helper.rb:84:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:83:in `block (2 levels) in <top (required)>'
Now this technically makes sense because my Site model has validates :url, :site_code, presence: true. So the test is failing because the post is not passing the params correctly.
Lastly, here is the controller:
module Api::V1
class SitesController < BaseApiController
before_action :set_site, only: [:show, :update, :destroy]
# GET /sites
def index
#sites = Site.all
render json: #sites
end
# GET /sites/1
def show
render json: #site
end
# POST /sites
def create
#site = Site.new(site_params)
if #site.save
render json: #site, status: :created, location: #site
else
render json: #site.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /sites/1
def update
if #site.update(site_params)
render json: #site
else
render json: #site.errors, status: :unprocessable_entity
end
end
# DELETE /sites/1
def destroy
#site.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_site
#site = Site.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def site_params
# params.require(:data).require(:attributes).permit(:url, :side_code, :user_id)
# params.require(:site).permit(:url, :side_code, :user_id)
params.fetch(:site, {}).permit(:url, :side_code)
end
end
end
I am speculating that the way I am passing parameters to the post for Rails API is perhaps not formatted or correctly or something else entirely. I did play with the params in the test block trying data: { attributes: valid_attributes } with no luck.
Any thoughts or suggestions are greatly appreciated!
This problem was indeed due to format of the parameters I was passing to POST request in the test block. I tested the POST via the command line and watched the rails server to see how the params were coming through. They looked like this:
Parameters: {"site_code"=>"123456", "url"=>"www.updated.com", "subdomain"=>"api", "id"=>"2", "site"=>{"site_code"=>"123456", "url"=>"www.updated.com"}}
Then in my sites_spec.rb, I copied this format for the valid params of the post request:
let(:valid_attributes) { { "site"=>{"url"=>"www.example.com", "user_id"=>user_id, "site_code"=>"123456"} } }
This works. The JSON format of the params needed to be formatted in the test block the same way they would be if it was real JSON request.

*** NoMethodError Exception: undefined method `to_sym' for nil:NilClass in Rails 3.2 with Rspec in controllers spec

I have configured Rspec Ruby 2.0.0-p0 and Rails 3.2.14 configuration is perfect I'm sure on that but when I try to run rake spec:controllers it gaves me below error on every request action written in spec example -
*** NoMethodError Exception: undefined method `to_sym' for nil:NilClass
I have written specs for controllers before but never come across such situation, help me If any one has fixed same issue....
Here is my spec and error stack
describe UsersController do
before (:each) do
#user = FactoryGirl.create(:user)
sign_in #user
end
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
describe "GET 'show'" do
it "should be successful" do
get :show, :id => #user.id
response.should be_success
end
it "should find the right user" do
get :show, :id => #user.id
assigns(:user).should == #user
end
end
end
Here is result -
Failures:
1) UsersController GET 'index' should be successful
Failure/Error: get 'index'
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
2) UsersController GET 'show' should be successful
Failure/Error: get :show, :id => #user.id
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
3) UsersController GET 'show' should find the right user
Failure/Error: get :show, :id => #user.id
NoMethodError:
undefined method `to_sym' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
Finished in 0.4741 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'index' should be successful
rspec ./spec/controllers/users_controller_spec.rb:20 # UsersController GET 'show' should be successful
rspec ./spec/controllers/users_controller_spec.rb:25 # UsersController GET 'show' should find the right user
Randomized with seed 19701
My factory is -
FactoryGirl.define do
factory :user do
first_name 'Test User'
last_name 'Last name'
email 'example#example.com'
password 'changeme'
password_confirmation 'changeme'
company 'RR'
confirmed_at Time.now
end
end
We need company name mandatory to create register user.
class UsersController < ApplicationController
before_filter :authenticate_user!
#authorize_resource
def index
#users = User.all
end
def show
#user = User.find(params[:id])
end
end
here I have added two methods for test in user controller.
Same thing I tried with demo example that works great but not in my project....
Thanks
If you are using devise gem for authentication then you need to specify the devise mapping in controller test cases.
Replace the code in the before(:each) block with below code
#user = FactoryGirl.create(:user)
#request.env['devise.mapping'] = Devise.mappings[:user]
sign_in #user
Finally I fixed issue, it comes due to authenticate_user! method in application controller which inherits to every controllers -
prepend_before_filter :authenticate_user!, :except => [:not_authenticated]
I have added below module to fix issue occurs due to this devise method -(support/controllers_helpers.rb)
module ControllerHelpers
def sign_in(user = double('user'))
if user.nil?
request.env['warden'].stub(:authenticate!).
and_throw(:warden, {:scope => :user})
controller.stub :current_user => nil
else
request.env['warden'].stub :authenticate! => user
controller.stub :current_user => user
end
end
end
finally include above module in spec_helper.rb file
config.include ControllerHelpers, :type => :controller
Got running all controllers specs.
Cheers!!!
more info

Rails Tests Not Found - Undefined local variable or method "response"

I'm trying to follow Michael Hartl's Rails 3 tutorial and I'm running into an error I can't find explained elsewhere. Under Section 9.2.1, I'm getting an error when trying to run a test for non-signed-in users trying to update a profile page.
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
The error I'm getting is "undefined local variable or method 'response' for # (NameError)". This is a response to "bundle exec rspec spec/". When I comment out the offending line, all tests instantiate and pass. I've tried replacing "response" with things like "response.body" and "page" but neither seem to help.
I'm not sure what the issue is because as I'm moving forward with the tutorial, similar blocks are passing without incident.
Here's the full error text:
C:\Ruby193\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\Ruby193\bin\rake spec
Testing started at 7:39 PM ...
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:60:in `block (5 levels) in <top (required)>': undefined local variable or method `response' for #<Class:0x4894908> (NameError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:58:in `block (4 levels) in <top (required)>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:51:in `block (3 levels) in <top (required)>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:48:in `block (2 levels) in <top (required)>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:46:in `block in <top (required)>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `module_eval'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:244:in `subclass'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:230:in `describe'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/dsl.rb:18:in `describe'
from C:/Users/Jeff/Documents/rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:3:in `<top (required)>'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
rake aborted!
C:/Ruby193/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb failed
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => spec
(See full trace by running task with --trace)
Process finished with exit code 1
Empty test suite.
I'm pretty sure it's not a typo somewhere but here's the offending file (authentication_pages_spec.rb). (have_title() is a method I found here for fixing an error because apparently have_selector('title',...) is no longer valid syntax.)
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it {should have_selector('h1', text: 'Sign in') }
it {should have_title('Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_title('Sign in') }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify ( response.should redirect_to(signin_path) )
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
before { sign_in user }
describe "visiting Users#edit_page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_title(full_title("Edit user")) }
end
describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) }
end
end
end
end
end
end
What version of Capybara are you running?
$ bundle exec gem list | grep capybara
If you're running version >= 2.0 you should look again at your Gemfile and specifically mark the version to be the same as the tutorial (1.1.2).
Gemfile
group :test do
gem 'capybara', '1.1.2'
end
If you desperately want to use the latest version, you'll have to make some changes, outlined in the following StackOverflow Q&As:
Capybara methods are undefined
capybara: post, get methods not working when changing name of requests directory to features
RSpec & Capybara 2.0 tripping up my have_selector tests
Update
Okay, you're going to kick yourself, but after cloning your repo and getting the same error, your problem is obvious (syntax highlighting is your friend):
On Line 77 of your spec/requests/authentication_pages_spec.rb you have:
specify ( response.should redirect_to(signin_path) )
It should be:
specify { response.should redirect_to(signin_path) }
After fixing that, your tests will pass.

Rspec what is wrong with my test for create controller action?

I am trying to learn TDD and this is part of my homework I couldn't figure out how to do it.
I want to test create controller action, and here is my code for test:
require 'spec_helper'
describe MoviesController do
describe 'create' do
it 'should call the model method perform create!' do
Movie.should_receive(:create!).with({"title" => 'Milk', "rating" => 'R'})
post :create, :movie => {:title => 'Milk', :rating => 'R'}
end
end
end
But I got:
Failures:
1) MoviesController create should call the model method performe create!
Failure/Error: post :create, :movie => {:title => 'Milk', :rating => 'R'}
NoMethodError:
undefined method `title' for nil:NilClass
# ./app/controllers/movies_controller.rb:50:in `create'
# ./spec/controllers/movies_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.21714 seconds
And here is create action I am testing against. Yes, it is TDD, and yes, I am testing a
working code, and it is the testing doesn't working :D
def create
#movie = Movie.create!(params[:movie])
flash[:notice] = "#{#movie.title} was successfully created."
redirect_to movies_path
end
I don't even know why I got the undefined method error message? I have a few other test passed but I deleted in this code snippe for simplicity, so I don't think it is related db/model related config problem. But why it does not working and how to change it?
Cheers
When you do a should_receive like that, it will return nil so the next line (when setting the flash message) attempts to retrieve the title from nil. Change your should_receive to:
Movie.should_receive(:create!).with({"title" => 'Milk', "rating" => 'R'}).and_return(stub_model(Movie))

Unexpected rspec behavior Ruby on Rails Turorial chapter 12

I am seeing an error that is only dependent on the location of the line:
should change(Relationship, :count).by(-1)
For example, with the code:
it "should destroy a relationship using Ajax" do
lambda do
xhr :delete, :destroy, :id => #relationship
response.should be_success
end.should change(Relationship, :count).by(-1) #<<-line is here
I get the rspec error:
1) RelationshipsController DELETE 'destroy' should destroy a relationship using Ajax
Failure/Error: xhr :delete, :destroy, :id => #relationship
ActionView::MissingTemplate:
Missing template relationships/destroy with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:js, :html], :locale=>[:en, :en]} in view paths "#<RSpec::Rails::ViewRendering::PathSetDelegatorResolver:0x00000100a5b5f8>"
# ./app/controllers/relationships_controller.rb:16:in `destroy'
# ./spec/controllers/relationships_controller_spec.rb:44:in `block (4 levels) in <top (required)>'
# ./spec/controllers/relationships_controller_spec.rb:43:in `block (3 levels) in <top (required)>'
But with the code:
it "should destroy a relationship using Ajax" do
lambda do
xhr :delete, :destroy, :id => #relationship
response.should be_success
should change(Relationship, :count).by(-1) #<<-Line moved to here
end
... the test passes.
I am seeing expected behavior when I use a web browser. Unfollowing a user adjusts totals correctly.
So, are these two rspec tests not equivalent? Am I falsely reassured that the second test passes? If they are functionally equivalent, why does the first one fail?
Seems this was due, in part, to having confused
./app/controllers/relationships_controller.rb
with
./spec/controllers/relationships_controller_spec.rb
Code pasted into the wrong file (appropriately) generated no syntax errors. Fixing my error lead to passing rspec tests.