Ruby on rails RubyonRails 4测试失败(删除功能)

Ruby on rails RubyonRails 4测试失败(删除功能),ruby-on-rails,Ruby On Rails,我在运行bundle exec rake测试时遇到故障。所讨论的文件是test/integration/users\u index\u test.rb。如下所示: require 'test_helper' class UsersIndexTest < ActionDispatch::IntegrationTest def setup @admin = users(:michael) @non_admin = users(:archer) end

我在运行bundle exec rake测试时遇到故障。所讨论的文件是test/integration/users\u index\u 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
_user.html.slim:

- provide(:title, 'All users')
h1
  | All users

= will_paginate

ul.users
  - @users.each do |user|
    = render @users

= will_paginate
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?" } %> 
li
=用户的gravatar_
=链接到user.name,user
-如果当前_user.admin?&&!当前用户?(用户)
|  |  

问题可能出在您的模板中:当您调用
render@users
时,Rails已经在
@users
上迭代了用户列表,因此您不需要
。每个部分都需要:

...
ul.users
  - @users.each do |user|
  = render @users
...
你可以这样做:

...
ul.users = render @users
...

我是一个新手,学习相同的教程,所以,不确定这是否是一个好的答案。让我知道它是否有用…

我也遇到了同样的问题,这是因为您的slim语法不好

修复方法是检查部分:_user.html.slim是否包含您试图在测试中断言的文本,在本例中,您的用户_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'
,也就是说,test正在index.html.slim中查找这样的文本,而index.html.slim中再次填充了您的partial user.html.slim(教程中对此有解释,因为
render@users
实际上是在partial中查找它们),并猜测您的slim语法有何错误

试试我的解决方案 \u 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?"}
li
=用户的gravatar_,尺寸:50
=链接到user.name,user
-如果当前_user.admin?&&!当前用户?(用户)

/下面这三行是我插入->“|”时遇到的错误

这是因为我在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 %>

所有用户

我的users.yml文件中有逗号。不知道我为什么要添加它们,但有一次我将它们取出,如果修复了我的问题

迈克尔: 姓名:Michael Example 电邮:michael@example.com 密码摘要: 管理员:是的 激活:正确 在以下位置激活\u:

vs

迈克尔: 姓名:Michael Example 电邮:michael@example.com 密码摘要: 管理员:是的, 是的, 在以下位置激活\u:


也遇到了同样的问题。对我来说,问题在于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') %>
dom:
名称:Dom示例
电邮:dom@example.com
密码摘要:
管理员:是的
dom:
姓名:詹姆斯·布朗
电邮:godfather@example.com
密码摘要:

视图是什么样子的?请仔细检查
users.yml
夹具文件,并确保您有
admin:true
一行用于
:michael
。michael用户在users.yaml文件中有admin true。视图已添加到上述问题中。还有其他想法吗?我不太熟悉slim语法,但如果要使用分部渲染@users
,则不应使用
@users.each do | user |
行。或者,您可以保留
每个
块和
渲染用户
(块的参数)。如果有帮助,请告诉我。
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') %>