Ruby on Rails 3.2.2, Rspec 2.9.0 및 RspecRails 2.9.0을 사용하고 있습니다. 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?
코드 라인에 관련) 얻을 new
행동에 관한 예 실행하면 :
Failure/Error: get :new
NoMethodError:
undefined method `new_record?' for nil:NilClass
을하지만, show
동작과 관련된 예제를 실행하면 오류없이 전달됩니다.
무엇이 문제입니까? 어떻게 해결할 수 있습니까?