简体   繁体   中英

Hartl Sample App Failure in integration test for assert_select a[href=?]

I have been working through the Hartl sample app course, and while I have had issues here and there, I have always been able to find answers, or identify the kink in my code by comparing with the tutorial git. However this time I am at a loss and am hoping someone in the community can figure out where I may have gone wrong.

I am getting the following minitest error:

    Minitest::Assertion: Expected at least 1 element matching "a[href="/users/14035331"]", found 0..
    Expected 0 to be >= 1.
    test/integration/users_index_test.rb:15:in `block (2 levels) in <class:UsersIndexTest>'
    test/integration/users_index_test.rb:14:in `block in <class:UsersIndexTest>'

Here is the code for the test which is exactly as it appears in the tutorial:

    require 'test_helper'

    class UsersIndexTest < ActionDispatch::IntegrationTest

      def setup
       @user = users(:james)
      end

      test "index including pagination" do
        log_in_as(@user)
        get users_path
        assert_template 'users/index'
        assert_select 'div.pagination'
        User.paginate(page: 1).each do |user|
           assert_select 'a[href=?]', user_path(user), text: user.name
        end
      end
    end

User 14035331 is the ID of the first record in the test.db. Possible issues I can think of but haven't been successful in identifying - User is not on Page 1 (this shouldn't be since it is the first ID record); the test is looking for the NAME field but getting the ID field.

I did find a similar question on here, but it doesn't address this problem. You can see the thread here: Got failure in integration testing of rails

That user is a bit further along, and I really hate to refractor and push forward when my test suite isn't passing.

Thanks to HASHROCKET I was able to determine that somehow my DB was sorted by ID DESC and my test expected it to be ASC. I am not sure how that happened exactly but I added the following line to the end of users.yml to force order by ID ASC and the test is green.

    <% User.order(id: :asc) %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM