1

我有以下步骤:

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>

我究竟做错了什么?

4

1 回答 1

2

黄瓜书,关于table#diff!方法:

它接受一个参数,它期望是一个表示行和列的 Array 的 Array。如果所有值都相等,则步骤定义通过。如果不是,则步骤定义失败并打印出差异。

因此,您需要将 Capybara 表映射到数组数组中,例如:

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

您可能必须对此进行试验——我想不出确切的 Capybara 方法来做到这一点。目的是将 Capybara 元素转换为数组数组,相当于:

table_result = [
    ['Sport Type', 'Zip Code', 'Description'],
    ['Extreme Ironing', '12345', 'Participants perform ironing tasks in improbably extreme surroundings'],
    # etc - whatever is on the page
]
于 2012-04-10T06:10:10.453 回答