doub1ejack doub1ejack - 1 month ago 12
Ruby Question

Confused by rspec feedback from assert_select test

I'm just getting going with rails and have a test for the index page for my

Plants
model:

The rspec test fails with this message:

$ rspec spec/views/plants/index.html.haml_spec.rb
Failures:

1) plants/index renders a list of plants
Failure/Error: assert_select "tr>td", :text => plant.height, :count => 2

Minitest::Assertion:
<48> expected but was
<Rose Mallow>..
Expected: 2
Actual: 0
# /Users/username/.rvm/gems/ruby-2.2.5/gems/minitest-5.9.1/lib/minitest/assertions.rb:139:in `assert'
# /Users/username/.rvm/gems/ruby-2.2.5/gems/minitest-5.9.1/lib/minitest/assertions.rb:174:in `assert_equal'
# /Users/username/.rvm/gems/ruby-2.2.5/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:278:in `assert_size_match!'
# /Users/username/.rvm/gems/ruby-2.2.5/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:175:in `block in assert_select'
# /Users/username/.rvm/gems/ruby-2.2.5/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:174:in `tap'
# /Users/username/.rvm/gems/ruby-2.2.5/gems/rails-dom-testing-1.0.7/lib/rails/dom/testing/assertions/selector_assertions.rb:174:in `assert_select'
# ./spec/views/plants/index.html.haml_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 0.19797 seconds (files took 3.07 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/views/plants/index.html.haml_spec.rb:9 # plants/index renders a list of plants


I guess I have two questions:


  1. Why is it looking for
    <48>
    ? Are the lt/gt symbols just wrapping the string it is looking for (which seems a poor delimiter choice), or is it really expecting an html element named "48"?

  2. And more importantly, why can't it find the two instances of the height? When I do
    put render
    in the test I see exactly two instances rendered:



rendered test markup:

<h1>Listing plants</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Scientific name</th>
<th>Height</th>
<th>Width</th>
<th>Spacing</th>
<th>Life cycle</th>
<th>Sun</th>
<th>Sow method</th>
<th>Direct seed start</th>
<th>Direct seed stop</th>
<th>Indoor seed start</th>
<th>Indoor seed stop</th>
<th>Transplant start</th>
<th>Transplant stop</th>
<th>Garden</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Rose Mallow</td>
<td>Lavatera trimestris</td>
<td>48</td>
<td>18</td>
<td></td>
<td>annual</td>
<td>full_sun</td>
<td>direct</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Default Test Garden</td>
<td><a href="/plants/1">Show</a></td>
<td><a href="/plants/1/edit">Edit</a></td>
<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/plants/1">Destroy</a></td>
</tr>
<tr>
<td>Rose Mallow</td>
<td>Lavatera trimestris</td>
<td>48</td>
<td>18</td>
<td></td>
<td>annual</td>
<td>full_sun</td>
<td>direct</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Default Test Garden</td>
<td><a href="/plants/1">Show</a></td>
<td><a href="/plants/1/edit">Edit</a></td>
<td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/plants/1">Destroy</a></td>
</tr>
</tbody>
</table>
<br>
<a href="/plants/new">New Plant</a>


Here's the test: spec/views/plants/index.html.haml_spec.rb

require 'rails_helper'

RSpec.describe "plants/index", type: :view do
before(:each) do
plant = FactoryGirl.create(:plant_rose_mallow)
assign(:plants, [ plant, plant ])
end

it "renders a list of plants" do
render
plant = FactoryGirl.create(:plant_rose_mallow)
puts plant.inspect
assert_select "tr>td", :text => plant.name, :count => 2
assert_select "tr>td", :text => plant.scientific_name, :count => 2
assert_select "tr>td", :text => plant.height, :count => 2
assert_select "tr>td", :text => plant.life_cycle, :count => 2
assert_select "tr>td", :text => plant.sun, :count => 2
assert_select "tr>td", :text => plant.sow_method, :count => 2
end
end


Here's my factory: spec/factories/plants.rb

FactoryGirl.define do
factory :plant do
association :garden, :factory => :garden

factory :plant_rose_mallow do
name "Rose Mallow"
scientific_name "Lavatera trimestris"
height 48
width 18
life_cycle :annual
sun :full_sun
sow_method :direct
end
end
end

Answer

Change your assert_select to use a string instead of an integer:

assert_select "tr>td", :text => plant.height.to_s, :count => 2

Your errors are a result of it searching for an integer and this will resolve them.