2016-11-07 62 views
2

我剛開始與軌持續和有索引頁的測試我Plants型號:從assert_select測試困惑通過rspec的反饋

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> 

這裏的測試:規格/視圖/植物/ 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 

這裏是我廠:規格/工廠/ 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 

回答

1

更改assert_select使用的不是整數的字符串:

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

你的錯誤是它尋找一個整數的結果,這將解決這些問題。

+0

好的,這是有道理的,謝謝。 – doub1ejack