简体   繁体   中英

Ruby Rails RSpec Local Variables

I use AJAX to filter a list of responses in my Index action and I am unsure how to test this.

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 code:

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 output:

Failures:

  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

UPDATED: So it turns out that the code above was wrong and that's what Rspec was trying to tell me. I updated the code to work properly and now the error above is what I get. I'm not sure how to assign a Pedigree object for each Trait object when it's created with assign(:traits, [stub_model(Trait), stub_model(Trait)]).

The problem is with your use of stub_model . In your view, @traits is a list of stub_model(Trait) instances. However you don't set a person on those instances, so in _list.html.erb on line 16 you're trying to call #pedigree on nil .

Try something like the following in the before block in 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

Also have a look at the stub_model documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM