Rails Minitest集成测试失败:预期至少有1个元素匹配"div.pagination",发现0 [英] Rails Minitest integration test broken: Expected at least 1 element matching "div.pagination", found 0
问题描述
我们以Michael Hartl的Rails教程为灵感来构建一个完全不同的RoR应用程序.
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.
这是我们的user_index_test.rb
文件:
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
这是我们的index.html.erb
文件:
<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>
哪个在浏览器中呈现以下HTML:
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>'
有什么不好的主意吗?
更新:根据@steve klein的建议(根据评论),我们更新了users_index_test.rb文件:
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
这是测试失败的结果:
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>'
推荐答案
我也收到了此失败的测试消息,结果发现我犯了一个有趣的错误:我忘记了将伪造的用户名"michael"
更改为我自己的测试名. ../test/fixtures/microposts.yml
.
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
如果您总是像我一样更改变量名,请检查它是否生成了足够的微博以进行测试.确保user_index_test.rb
中的@admin
值等于microposts.yml
中的user: your_admin_user_name
.
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
.
希望此帮助=)
这篇关于Rails Minitest集成测试失败:预期至少有1个元素匹配"div.pagination",发现0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!