【问题标题】:render failing in rspec test for view在 rspec 测试中渲染失败以供查看
【发布时间】:2013-11-07 09:51:45
【问题描述】:

在我看来,规格如下

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

我很困惑为什么我的页面确实包含文本时会出现此错误。

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      相关资源
      最近更新 更多