在视图的rspec测试中渲染失败

时间:2013-11-07 09:51:46

标签: ruby-on-rails ruby-on-rails-3 rspec bdd

在我看来,我有以下内容

require 'spec_helper'

describe "my_project/index.html.erb" do
  it "displays the Country drop down" do
    render
    rendered.should contain("Country")
  end
end

但是当我运行规范时我得到以下错误

Failure/Error: render
 ActionView::Template::Error:
   undefined method `map' for nil:NilClass

我很困惑,为什么当我的页面确实包含文本时,我收到此错误。

1 个答案:

答案 0 :(得分:2)

您是否需要一个实例变量才能使用它?即,在控制器索引方法中设置了什么......所以像这样:

require 'spec_helper'

describe "my_project/index.html.erb" do
  before do
    @some_instance_variable = SomeClass.all
    render
  end

  it "displays the Country drop down" do
    rendered.should contain("Country")
  end
end
相关问题