2011-04-07 52 views
3

再一次,似乎卡住了簡單的視圖測試。這裏的代碼:RSpec + Factory_Girl查看測試

require 'spec_helper' 

describe "posts/show.html.erb" do 
    setup do 
    @post = Factory(:post) 
    end 

    it "should not render a canonical link rel" do 
    get :show, :id => @post.id 
    response.should_not have_tag("link[rel=?]", "canonical") 
    end 
end 

當我從控制檯工廠(:後),沒有問題,完美地創建它。但是當我在命令行運行這個時,我得到以下內容:

rspec spec/views/posts/show.html.erb_spec.rb 
F 

Failures: 

    1) posts/show.html.erb should not render a canonical link rel 
    Failure/Error: get :show, :id => @post.id 
    RuntimeError: 
     Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id 
    # ./spec/views/posts/show.html.erb_spec.rb:11 

Finished in 0.06532 seconds 
1 example, 1 failure 

任何想法我失蹤?

回答

5

你想用before each

before(:each) do 
    @post = Factory(:post) 
end 

before do 
    @post = Factory(:post) 
end 

的簡寫。

而不是設置塊。

+0

哇,那簡直太神奇了。那麼設置塊是什麼? – aronchick 2011-04-07 21:57:15

+0

設置塊用於測試/單元框架 – 2011-06-06 22:40:00