C
C
Ciscoridze2014-10-15 23:04:58
Ruby on Rails
Ciscoridze, 2014-10-15 23:04:58

How to count test the number of rows in a table?

Good afternoon.
There is a code:

<table class="table" id="charges_failed">
    <tr>
      <th>Customer</th>
      <th>Charge Amount</th>
      <th>Date Failed</th>
      <th colspan="3"></th>
    </tr>
    <% @charges_failed.each do |charge| %>
      <tr bgcolor="#FF0000">
        <td><%= charge.formatted_customer_name %></td>
        <td><%= number_to_currency(charge.amount.to_s) %></td>
        <td><%= charge.created_at.strftime("%d/%m/%Y") %></td>
      </tr>
    <% end %>
  </table>

It outputs 5 lines.
There is a test that should compare the number of rows with five:
it "check failed charges" do
    visit root_path
    expect(page).to have_selector('table#charges_failed tr', :count => 5)
end

But the test only finds the first row (expected to find css "table#charges_failed" 5 times, found 1 match: "Customer Charge Amount Date Failed")
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2014-10-16
@Jeiwan

Two different databases are used during development and testing. That is, in the development database, you have 5 records in the table, and the testing database is empty. Database settings can be viewed in config/database.yml
When testing, factories (factories, the factory_girl gem) are usually used, with the help of which the database is filled with the necessary records before each test. In your case, before each test, you need to create 5 records in the test database (another number is possible, the database for testing never matches the database for development). Search the web for articles about testing with factory_girl.
Also, your test selector is not written correctly, since it finds the table header. To solve this problem, in the layout, you can wrap the header row of the table in the thead tag and the data rows in the tbody tag and add tbody to the selector: "table#charges_failed tbody tr".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question