我刚开始使用rails并测试我的Plants
模型的索引页面:
rspec测试失败并显示以下消息:
$ 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
我想我有两个问题:
<48>
?是/ gt符号只是包装它正在寻找的字符串(这似乎是一个糟糕的分隔符选择),还是它真的期望一个名为“48”的html元素?put render
时,我看到正好呈现了两个实例:呈现测试标记:
<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>
以下是测试: 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
这是我的工厂: 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
答案 0 :(得分:1)
将assert_select更改为使用字符串而不是整数:
assert_select "tr>td", :text => plant.height.to_s, :count => 2
您的错误是搜索整数的结果,这将解决它们。