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

时间:2018-02-01 18:22:42

标签: ruby-on-rails

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.

1 个答案:

答案 0 :(得分:0)

感谢HASHROCKET,我能够确定我的DB按ID DESC排序,我的测试预计它是ASC。我不确定这是怎么发生的,但我在users.yml的末尾添加了以下行以强制按ID ASC排序,测试为绿色。

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