私はRuby on Rails 3.2.2、Rspec 2.9.0およびRspecRails 2.9.0を使用しています。私はnew
コントローラーアクションをテストしようとしています。なぜ、上記のエラーだけがそのアクションに対して説明されているのか知りたいのですが。「新しい」コントローラーアクションをテストするにはどうすればよいですか?
は考える:私はnew
アクションに関連した例を実行すると
# 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
私は(それはコントローラファイル内@article.new_record?
コード行に関連している)、このエラーが出る:
Failure/Error: get :new
NoMethodError:
undefined method `new_record?' for nil:NilClass
しかし、 show
アクションに関連する例を実行すると、エラーなしで渡されます。
何が問題ですか?それをどうすれば解決できますか?