我遇到运行bundle exec rake test的失败。有问题的文件是test / integration / users_index_test.rb。它看起来如下:
require 'test_helper'
class UsersIndexTest < ActionDispatch::IntegrationTest
def setup
@admin = users(:michael)
@non_admin = users(:archer)
end
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.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
end
assert_difference 'User.count', -1 do
delete user_path(@non_admin)
end
end
test "index as non-admin" do
log_in_as(@non_admin)
get users_path
assert_select 'a', text: 'delete', count: 0
end
end
运行rake测试,我得到以下内容&#34;失败&#34;:
1) Failure:
UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links [/Users/********/workspace/myapp/test/integration/users_index_test.rb:19]:
<delete> expected but was
<User 19>.
Expected 0 to be >= 1.
19 runs, 49 assertions, 1 failures, 0 errors, 0 skips
任何想法有什么不对?感谢。
查看(index.html.slim):
- provide(:title, 'All users')
h1
| All users
= will_paginate
ul.users
- @users.each do |user|
= render @users
= will_paginate
_user.html.slim:
li
= gravatar_for user
= link_to user.name, user
- if current_user.admin? && !current_user?(user)
| | <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %>
答案 0 :(得分:3)
问题可能在您的模板中:当您致电render @users
时,Rails已遍历@users
处的用户列表,因此您不需要.each do
部分:
...
ul.users
- @users.each do |user|
= render @users
...
你可以这样做:
...
ul.users = render @users
...
我是同一个教程的新手,因此,不确定它是否是一个好的答案。如果它有帮助,请告诉我......
答案 1 :(得分:2)
得到了完全相同的问题。对我来说问题出在users.yml文件中。我在测试中登录的管理员用户在 user.yml文件中有重复的条目。
dom:
name: Dom Example
email: dom@example.com
password_digest: <%= User.digest('password') %>
admin: true
dom:
name: James Brown
email: godfather@example.com
password_digest: <%= User.digest('password') %>
答案 2 :(得分:0)
我遇到了同样的问题,因为你的语法不够好。
修复方法是检查部分: _user.html.slim 是否包含您尝试在测试中断言的文本,在本例中为 users_index_test.rb 包含:
first_page_of_users.each do |user|
assert_select 'a[href=?]', user_path(user), text: user.name
unless user == @admin
assert_select 'a[href=?]', user_path(user), text: 'delete',
method: :delete
end
end
您正在声明text: 'delete'
,也就是说,测试正在 index.html.slim 中查找此类文本,该文本再次填充了您的部分 _user.html.slim (教程中对此进行了解释,因为render @users
实际上是在部分中查找它们)并猜测你的语法错误。
尝试我的解决方案 的 _user.html.slim 强>:
li
= gravatar_for user, size: 50
= link_to user.name, user
- if current_user.admin? && !current_user?(user)
/These 3 lines below are my slim hack for inserting -> " | " <- in front of "delete" /!
=<
|
|
=< link_to "delete", user, method: :delete, data: {confirm: "You sure?"}
railstutorial.org by mhartl(为了搜索而添加)
答案 3 :(得分:0)
我有完全相同的错误。
这是因为我在index.html.erb中放置了_user.html.erb的代码。确保您的_user.html.erb正确无误。
index.html.erb
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>
答案 4 :(得分:0)
我的users.yml文件中有逗号。不知道我为什么要添加它们,但是如果我解决了问题就把它们拿出来了。
迈克尔: 名称:Michael示例 电子邮件:michael@example.com password_digest:&lt;%= User.digest(&#39;密码&#39;)%&gt; admin:true 激活:是的 activated_at:&lt;%= Time.zone.now%&gt;
VS
迈克尔: 名称:Michael示例 电子邮件:michael@example.com password_digest:&lt;%= User.digest(&#39;密码&#39;)%&gt; admin:true, 激活:是的, activated_at:&lt;%= Time.zone.now%&gt;