Search code examples
ruby-on-rails-4paginationintegration-testingrailstutorial.orgwill-paginate

Rails Minitest integration test broken: Expected at least 1 element matching "div.pagination", found 0


We are using Michael Hartl's Rails Tutorial as an inspiration to build a completely different RoR app.

However, we stumbled upon an issue that may be of help to other people following the tutorial.

Here is our user_index_test.rb file:

test "index as admin including pagination and delete links" do
    log_in_as(@admin)
    get users_path
    assert_select 'div.pagination'
    first_page_of_users = User.paginate(page: 1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.first_name + " " + user.first_name
      unless user == @admin
        assert_select 'a[href=?]', user_path(user), text: 'delete'
      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

Here is our index.html.erb file:

<h1>All users</h1>

<div class="row">

    <div class="pagination col-md-6 col-md-offset-3">

        <%= will_paginate %>

        <ul class="users">
          <% @users.each do |user| %>
            <%= render user %>
          <% end %>
        </ul>

        <%= will_paginate %>

    </div>

</div>

Which renders the following HTML in the browser:

<div class="row">

    <div class="col-md-6 col-md-offset-3">

        <div class="pagination"><ul class="pagination"><li class="prev previous_page disabled"><a href="#">← Previous</a></li> <li class="active"><a rel="start" href="/users?page=1">1</a></li> <li><a rel="next" href="/users?page=2">2</a></li> <li><a href="/users?page=3">3</a></li> <li><a href="/users?page=4">4</a></li> <li><a href="/users?page=5">5</a></li> <li><a href="/users?page=6">6</a></li> <li><a href="/users?page=7">7</a></li> <li><a href="/users?page=8">8</a></li> <li><a href="/users?page=9">9</a></li> <li><a href="/users?page=10">10</a></li> <li><a href="/users?page=11">11</a></li> <li class="next next_page "><a rel="next" href="/users?page=2">Next →</a></li></ul></div>

        <ul class="users">
            <li>
  <a href="/users/1">Billy Joel</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/1">delete</a>
</li>
            <li>
  <a href="/users/2">Stevie Wonder</a>
</li>
            <li>
  <a href="/users/3">Michael Jackson</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/3">delete</a>
</li>
            <li>
  <a href="/users/4">Sterling Archer</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/4">delete</a>
</li>
            <li>
  <a href="/users/5">Telly Miller</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/5">delete</a>
</li>
            <li>
  <a href="/users/6">Elvie Lindgren</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/6">delete</a>
</li>
            <li>
  <a href="/users/7">Kianna Beier</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/7">delete</a>
</li>
            <li>
  <a href="/users/8">Wilhelmine Wuckert</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/8">delete</a>
</li>
            <li>
  <a href="/users/9">Blanche Moore</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/9">delete</a>
</li>
            <li>
  <a href="/users/10">Hailey Jacobson</a>
    | <a data-confirm="You sure?" rel="nofollow" data-method="delete" href="/users/10">delete</a>
</li>
        </ul>

        <div class="pagination"><ul class="pagination"><li class="prev previous_page disabled"><a href="#">← Previous</a></li> <li class="active"><a rel="start" href="/users?page=1">1</a></li> <li><a rel="next" href="/users?page=2">2</a></li> <li><a href="/users?page=3">3</a></li> <li><a href="/users?page=4">4</a></li> <li><a href="/users?page=5">5</a></li> <li><a href="/users?page=6">6</a></li> <li><a href="/users?page=7">7</a></li> <li><a href="/users?page=8">8</a></li> <li><a href="/users?page=9">9</a></li> <li><a href="/users?page=10">10</a></li> <li><a href="/users?page=11">11</a></li> <li class="next next_page "><a rel="next" href="/users?page=2">Next →</a></li></ul></div>

    </div>

</div>

Yet, here is the result of the integration test:

FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 2015-06-30 06:44:20 -0700]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1435671860.33s)
        Expected at least 1 element matching "div.pagination", found 0..
        Expected 0 to be >= 1.
        test/integration/users_index_test.rb:23:in `block in <class:UsersIndexTest>'

Any idea of what is wrong?

UPDATE: following @steve klein advice (from the comments), we updated our users_index_test.rb file:

test "index as admin including pagination and delete links" do
    log_in_as(@admin)
    get users_path
    assert_template 'users/index'
    assert_select 'div.pagination'
    first_page_of_users = User.paginate(page: 1)
    first_page_of_users.each do |user|
      assert_select 'a[href=?]', user_path(user), text: user.first_name + " " + user.first_name
      unless user == @admin
        assert_select 'a[href=?]', user_path(user), text: 'delete'
      end
    end
    assert_difference 'User.count', -1 do
      delete user_path(@non_admin)
    end
  end

Here is the resulting, failing test:

FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 2015-06-30 06:44:19 -0700]
 test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1435671859.86s)
        expecting <"users/index"> but rendering with <[]>
        test/integration/users_index_test.rb:23:in `block in <class:UsersIndexTest>'

Solution

  • I also got this failed test message and found out that I made a funny mistake: I forgot to change fake user's name "michael" to my own test name in ../test/fixtures/microposts.yml.

    SO I DIDN'T GENERATE ENOUGH FAKE MICROPOSTS

    If you always change variable name just like me, please check it you generate enough microposts for test. Make sure your value of @admin in user_index_test.rb is equal to user: your_admin_user_name in microposts.yml.

    Hope this help =)