2012-04-10 62 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