测试使用水豚将用户放入 table html 并期待 xpath

Test of put users in a table html with capybara and expect an xpath

我在 rails 5.1 上 ruby 有一个用户 table,它是这样的:

<table id="users" class="table table-bordered">
  <thead>
    <tr>
      <th></th>
      <th>ID</th>
      <th>Nombre</th>
      <th>Apellidos</th>
      <th>Correo electrónico</th>
      <th colspan="2">Registro</th>
    </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td>i</td>
        <td><%= user.id %></td>
        <td><%= user.name %></td>
        <td><%= user.last_name %></td>
        <td><%= user.email %></td>
        <td><%= user.created_at.hour %>:<%= user.created_at.min%> hrs.</td>
        <td><%= user.created_at.to_date %></td>
      </tr>
    <% end %>
  </tbody>
</table>

情况是我正在使用水豚、rspec 和 factorybot 进行测试,我需要测试我正在创建 10 个用户并将其添加到 table,之后我希望我的测试有一个 xpath,如下所示:

 scenario 'admin view users' do
    visit admin_users_path
    users = create_list(:user, 10)
    expect(page).to have_xpath('//*[@id="users"]/tbody/tr/td[5]', text: "#{users.first.email}" )
  end 

问题是我收到 expect 的错误,我的假设是 xpath 是正确的,但有些东西无法写入或测试用户table里面。

我的问题是:

我如何 运行 创建 10 个用户时进行测试(例如),然后将其添加到 table 以便后记正确地期望 xpath?

我找到了解决方案:

显然,我是在访问该页面后创建用户的,所以该视图从未接收到数据。我能够通过更改用户的创建顺序来解决它,然后访问该页面,然后我可以正确地期待 xpath。

scenario 'admin view users' do
   users = create_list(:user, 10)
   visit admin_users_path
   expect(page).to have_xpath('//*[@id="users"]/tbody/tr/td[5]', text: "#{users.first.email}" )
end