I am using Ruby on Rails 3.2.2, Rspec 2.9.0 and RspecRails 2.9.0. I am trying to test a new controller action and I would like to know why I get the error explained above only for that action.
Given:
# controller
class ArticlesController < ApplicationController
before_filter :signed_in
def new
@article = Article.new
# This is just a sample code line to show you where the error happens?
@article.new_record?
...
end
def show
@article = Article.find(params[:id])
...
end
end
# spec file
require 'spec_helper'
describe ArticlesController do
before(:each) do
@current_user = FactoryGirl.create(:user)
# Signs in user so to pass the 'before_filter'
cookies.signed[:current_user_id] = {:value => [@current_user.id, ...]}
end
it "article should be new" do
article = Article.should_receive(:new).and_return(Article.new)
get :new
assigns[:article].should eq(article)
end
it "article should be shown" do
article = FactoryGirl.create(:article)
get :show, :id => article.id.to_s
assigns[:article].should eq(article)
end
end
When I run the Example related to the new action I get this error (it is related to the @article.new_record? code line in the controller file):
Failure/Error: get :new
NoMethodError:
undefined method `new_record?' for nil:NilClass
But when I run the Example related to the show action it passes without errors.
What is the problem? How can I solve that?
The problem is the way you’ve done
This is the same as
So by the time you are setting up the return value,
Article.newhas already been mocked out and so returns nil, so you’re doingand_return(nil)Create the return value first, i.e.