1

Index アクションで 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>

_list.html.erb:

<% 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 %>

index.js.erb

$("#filter_table").replaceWith("<div id=\"filter_table\"><%= escape_javascript(render 'list', :traits => @traits) %></div>")
$(".tablesorter").tablesorter({widgets: ['zebra']});

特性コントローラー:

 def index
    @traits = Trait.has_pedigree(params[:pedigree_filter]).has_person(params[:person])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @traits }
      format.js
    end
  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

更新: 上記のコードが間違っていたことが判明し、それが Rspec が私に伝えようとしていたことです。正しく動作するようにコードを更新したところ、上記のエラーが発生しました。assign(:traits, [stub_model(Trait), stub_model(Trait)]) で作成された各 Trait オブジェクトに Pedigree オブジェクトを割り当てる方法がわかりません。

4

1 に答える 1

1

問題は、の使用にありstub_modelます。あなたの見解では、はインスタンス@traitsのリストです。stub_model(Trait)ただし、これらのインスタンスに人を設定しないため、_list.html.erb16行目でを呼び出そうとしてい#pedigreeますnil

beforeのブロックで次のようなものを試してくださいindex.html.erb_spec.rb

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

stub_modelドキュメントもご覧ください。

于 2011-11-19T09:36:57.620 に答える