再次,似乎停留在简单的视图测试上。这是代码:
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
当我从控制台进行Factory(:post)时,没有任何问题,可以完美地创建它。但是当我在命令行上运行它时,我得到以下结果:
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
我缺少什么想法?
答案 0 :(得分:5)
您想使用before each:
before(:each) do
@post = Factory(:post)
end
或
before do
@post = Factory(:post)
end
简写。
而不是设置块。