2011-11-03 116 views
1

我使用AJAX筛选我的索引操作中的响应列表,我不确定如何测试此操作。Ruby Rails RSpec局部变量

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']}); 

traits_controller:

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对象。

+0

鉴于上面的代码,'_list.html.erb'如何知道'traits'是什么?你可以在渲染调用中发布包含':locals => {:traits => @traits}'的错误吗? –

+0

你是完全正确的。我在index.html.erb中修复了渲染调用,并更新了我收到的错误。 –

回答

1

问题出在您使用stub_model。在您看来,@traitsstub_model(Trait)实例的列表。但是,您不会在这些实例上设置人员,因此在第16行的_list.html.erb中,您正尝试致电 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 documentation