Ruby on rails Ruby-Rails-RSpec局部变量

Ruby on rails Ruby-Rails-RSpec局部变量,ruby-on-rails,ruby,rspec,render,local-variables,Ruby On Rails,Ruby,Rspec,Render,Local Variables,我在索引操作中使用AJAX过滤响应列表,我不确定如何测试这一点 index.html.erb: <h1>Listing traits</h1> <%= render "partials/filter" %> <%= link_to 'New Trait', new_trait_path %> <div id="filter_table"> <%= render 'list', :traits => @traits %&g

我在索引操作中使用AJAX过滤响应列表,我不确定如何测试这一点

index.html.erb:

<h1>Listing traits</h1>
<%= render "partials/filter" %>
<%= link_to 'New Trait', new_trait_path %>
<div id="filter_table">
  <%= render 'list', :traits => @traits %>
</div>
<% if traits.size > 0 %>
<table class="tablesorter">
  <thead>
  <tr>
    <th>Pedigree</th>
    <th>Person</th>
    <th>Phenotype</th>
    <th>Value</th>
    <th>Output order</th>
    <th class="nosort">controls</th>
  </tr>
  </thead>
  <tbody>
<% traits.each do |trait| %>
  <tr>
    <td><%= trait.person.pedigree.name %></td>
    <td><%= trait.person.identifier %></td>
    <td><%= trait.phenotype.name if trait.phenotype %></td>
    <td><%= trait.trait_information %></td>
    <td><%= trait.output_order %></td>
    <td><%= link_to 'Show', trait %></td>
  </tr>
<% end %>
</tbody>
</table>
<% else %>
  <p>No traits for person <%if params[:person] %><%= Person.find(params[:person]).full_identifier %><% end %></p>
<% end %>
Rspec代码:

require 'spec_helper'
describe "traits/index.html.erb" do
  before(:each) do
    @traits = assign(:traits, [
      stub_model(Trait),
      stub_model(Trait)
    ])
  end

  it "renders a list of traits" do
    render
  end
end
Rspec输出:

失败:

  1) traits/index.html.erb renders a list of traits
     Failure/Error: render
     ActionView::Template::Error:
       undefined method `pedigree' for nil:NilClass
     # ./app/views/traits/_list.html.erb:16:in `block in _app_views_traits__list_html_erb___3395464522456253198_189374900'
     # ./app/views/traits/_list.html.erb:14:in `each'
     # ./app/views/traits/_list.html.erb:14:in `_app_views_traits__list_html_erb___3395464522456253198_189374900'
     # ./app/views/traits/index.html.erb:11:in `_app_views_traits_index_html_erb__2914970758361867957_188338000'
     # ./spec/views/traits/index.html.erb_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.42509 seconds
1 example, 1 failure
1)traits/index.html.erb呈现特征列表
失败/错误:渲染
ActionView::模板::错误:
nil:NilClass的未定义方法“系谱”
#/app/views/traits/_list.html.erb:16:in'block in_app_view_traits_list_html_erb_3395464522456253198_189374900'
#/app/views/traits/_list.html.erb:14:in'each'
#/app/views/traits/\u list.html.erb:14:in``应用程序视图特征列表html\u erb\uuuu 3395464522456253198\u 189374900'
#./app/views/traits/index.html.erb:11:in`` app\u views\u traits\u index\u html\u erb\u 29149075836867957\u 188338000'
#./spec/views/traits/index.html.erb_spec.rb:12:in'block(2层)in'
以0.42509秒完成
1例,1例失败

更新:原来上面的代码是错误的,这就是Rspec想要告诉我的。我更新了代码以使其正常工作,现在我得到了上面的错误。我不确定在使用assign(:traits,[stub\u model(Trait),stub\u model(Trait)])创建每个Trait对象时,如何为其分配谱系对象。

问题在于使用
stub\u model
。在您看来,
@traits
存根模型(Trait)
实例的列表。但是,您没有在这些实例上设置person,因此在第16行的
\u list.html.erb
中,您试图调用
上的
\pedigree
nil

index.html.erb_spec.rb
中的
before
块中尝试以下操作:

before(:each) do
  person = stub_model(Person, :pedigree => 'something', :identifier => 'id') # etc.
  @traits = assign(:traits, [
    stub_model(Trait, :person => person),
    stub_model(Trait, :person => person)
  ])
end

另请查看。

问题在于您使用的是
存根\ U型
。在您看来,
@traits
存根模型(Trait)
实例的列表。但是,您没有在这些实例上设置person,因此在第16行的
\u list.html.erb
中,您试图调用
上的
\pedigree
nil

index.html.erb_spec.rb
中的
before
块中尝试以下操作:

before(:each) do
  person = stub_model(Person, :pedigree => 'something', :identifier => 'id') # etc.
  @traits = assign(:traits, [
    stub_model(Trait, :person => person),
    stub_model(Trait, :person => person)
  ])
end

另外,请看一下。

鉴于您的上述代码,
\u list.html.erb
如何知道
特征是什么?在渲染调用中包含
:locals=>{:traits=>@traits}
时,您是否可以发布错误?您完全正确。我修复了index.html.erb中的render调用,并更新了我得到的错误。鉴于上面的代码,
\u list.html.erb
如何知道
特征是什么?在渲染调用中包含
:locals=>{:traits=>@traits}
时,您是否可以发布错误?您完全正确。我修复了index.html.erb中的render调用,并更新了我得到的错误。
before(:each) do
  person = stub_model(Person, :pedigree => 'something', :identifier => 'id') # etc.
  @traits = assign(:traits, [
    stub_model(Trait, :person => person),
    stub_model(Trait, :person => person)
  ])
end