我们正在使用Michael Hartl的Rails教程作为构建完全不同的RoR应用程序的灵感。
但是,我们偶然发现了一个可能对本教程后的其他人有所帮助的问题。
以下是我们的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:
<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>
然而,这是集成测试的结果:
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文件:
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>'
答案 0 :(得分:1)
我也收到了这条失败的测试消息,发现我犯了一个有趣的错误:我忘了将假用户的名字"michael"
更改为../test/fixtures/microposts.yml
中我自己的测试名称。
我是否已经生成了足够的伪造微电影
如果您总是像我一样更改变量名称,请检查它是否生成足够的微博进行测试。确保@admin
中的user_index_test.rb
值等于user: your_admin_user_name
中的microposts.yml
。
希望这个帮助=)
答案 1 :(得分:0)
这似乎是Hartl教程的一个问题 - 这是我发现的第一个错字。我在使用Hartl的教程(第3版)时遇到了这个问题。这就是我修复它的方法。
我认为问题是users_index_test.rb
,当它在代码清单9.44中首次引入时,已经
@user = users(:michael)
setup
方法中的。但是下次我们在代码清单9.57中看到文件更新时,setup
方法改为使用两行:
@admin = users(:michael)
@non_admin = users(:archer)
注意有些变化?它之前是@user
,现在是@admin
。令人讨厌的是,哈特尔并没有像往常那样强调这些线条,因为它们是不同的。在这种情况下,你只需要注意区别。
所以基本上,无论你使用@user
还是@admin
,只要确保在导致错误的行中使用相同的变量,即
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
如果您完全按照代码清单9.57中的Hartl输入代码,并且始终使用@admin
,则不会收到错误。或者您可以将unless
块中的变量更改为@user
(如果这是setup
方法中的内容),那么它也可以使用。