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

强颜欢笑 提交于 2019-12-13 05:27:11

问题


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>'

回答1:


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 =)




回答2:


This appears to be a problem with Hartl's tutorial—the very first typo I've spotted. I just encountered this problem myself while working through Hartl's tutorial (3rd edition). Here's how I fixed it.

I think the problem is that users_index_test.rb, when it was first introduced in Listing 9.44, has

  @user = users(:michael)

in the setup method. But the very next time we see the file as updated in Listing 9.57, the setup method has instead the two lines:

  @admin     = users(:michael)
  @non_admin = users(:archer)

Notice something changed? It was @user before and is @admin now. The annoying thing is that Hartl didn't highlight these lines, as he usually does, as being different. In this case, you just had to notice the difference.

So basically, whether you use @user or @admin, just make sure you use that same variable in the lines that are causing the error, i.e.,

  unless user == @admin
    assert_select 'a[href=?]', user_path(user), text: 'delete',
                                                method: :delete
  end

If you type in the code exactly as Hartl has it in Listing 9.57, with @admin throughout, you won't get the error. Or you can change the variable in the unless block to @user (if that's what you have in your setup method), and that'll make it work too.



来源:https://stackoverflow.com/questions/31164919/rails-minitest-integration-test-broken-expected-at-least-1-element-matching-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!