gpt4 book ai didi

ruby-on-rails - 对来自 assert_select 测试的 rspec 反馈感到困惑

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:27 24 4
gpt4 key购买 nike

我刚开始使用 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

我想我有两个问题:

  1. 为什么要查找 <48> ? lt/gt 符号只是将它要查找的字符串包裹起来(这似乎是一个糟糕的分隔符选择),还是它真的需要一个名为“48”的 html 元素?
  2. 更重要的是,为什么它找不到高度的两个实例?当我做 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

最佳答案

更改您的 assert_select 以使用字符串而不是整数:

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

你的错误是它搜索整数的结果,这将解决它们。

关于ruby-on-rails - 对来自 assert_select 测试的 rspec 反馈感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com