2012-04-10 46 views
1

我有以下步驟:的Rails +黃瓜+水豚:與HTML表進行比較,預計表

Then I should see the following games: 
    | soccer  | 94040  | "friendly" | 
    | basketball | 94050  | "competition"| 

和我有以下步驟定義:

Then /^I should see the following games:$/ do |expected_table| 
    table_results = page.find('#games_results_table') 
end 

如果我做puts table_results我得到:

#<Capybara::Element tag="table" path="/html/body/div[2]/table"> 

我試過這樣做是爲了比較expected_table和table_results:

expected_table.diff!(table_results) 

但我得到這個錯誤:

undefined method `transpose' for #<Capybara::Element tag="table" path="/html/body/div[2]/table"> (NoMethodError) 

注意,正在呈現表中的看法是這樣的:

<div class="page-header"> 
    <h1>Games</h1> 
    <table id="games_results_table" class="table table-striped"> 
    <tr> 
     <th>Sport Type</th> 
     <th>Zip Code</th> 
     <th>Description</th> 
    </tr> 
     <% @games.each do |game| %> 
     <tr> 
      <td><%= game.sport_type %></td> 
      <td><%= game.zip_code %></td> 
      <td><%= game.description %></td> 
     </tr> 
     <% end %> 
    </table> 
</div> 

我在做什麼錯?

回答

2

the Cucumber book,在table#diff!方法:

It takes a single argument which it expects to be an Array of Array representing rows and columns. If all the values are equal, the step definition passes. If not, the step definition fails and a diff is printed out.

所以,你需要你的水豚表映射到一個數組的數組,像這樣:

table_results = page.find('#games_results_table tr').map do |row| 
    row.children.map do |cell| 
     cell.text 
    end 
end 

您可能必須與此試驗 - 我不能認爲水豚的確切方法就是這樣做的。其目的是把水豚元件成陣列相當於數組:

table_result = [ 
    ['Sport Type', 'Zip Code', 'Description'], 
    ['Extreme Ironing', '12345', 'Participants perform ironing tasks in improbably extreme surroundings'], 
    # etc - whatever is on the page 
] 
+0

此[要旨](https://gist.github.com/denmarkin/1334262)提供在比較表的一個工作示例您的功能添加到生成的HTML中的表格中。 – Ritchie 2014-06-15 09:51:02