![](/img/trans.png)
[英]Integration test: `assert_select 'a[href=?]'` fails for paginated pages
[英]Hartl Sample App Failure in integration test for assert_select a[href=?]
我一直在完成Hartl示例应用程序课程,尽管到处都有问题,但我始终能够通过与git教程进行比较来找到答案或识别代码中的纽结。 但是,这一次我很茫然,希望社区中的某人可以弄清楚我可能出了问题。
我收到以下最小测试错误:
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>'
这是测试的代码,与教程中显示的完全相同:
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
用户14035331是test.db中第一条记录的ID。 我能想到的但可能尚未成功识别的可能问题-用户不在页面1上(因为它是第一个ID记录,所以不应该存在); 测试正在寻找NAME字段,但获取ID字段。
我确实在这里找到了类似的问题,但是并没有解决这个问题。 您可以在此处看到该线程: Rails集成测试失败
该用户还有点距离,我真的很讨厌屈光检查并在我的测试套件未通过时继续前进。
多亏了HASHROCKET,我才能确定我的数据库是按ID DESC排序的,而我的测试期望它是ASC。 我不确定这到底是怎么发生的,但是我在users.yml的末尾添加了以下代码,以通过ID ASC强制排序,并且测试是绿色的。
<% User.order(id: :asc) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.