【问题标题】:RSpec + Factory_Girl View TestingRSpec + Factory_Girl 查看测试
【发布时间】:2011-08-01 01:29:38
【问题描述】:

再次,似乎被困在一个简单的视图测试上。代码如下:

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

任何想法我错过了什么?

【问题讨论】:

    标签: ruby-on-rails-3 testing rspec factory-bot


    【解决方案1】:

    你想使用before each:

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

    或

    before do
      @post = Factory(:post)
    end
    

    简写。

    而不是设置块。

    【讨论】:

    • 哇,这太简单了。那么设置块是什么?
    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多