gpt4 book ai didi

ruby-on-rails - Ruby on Rails 4 测试失败(删除功能)

转载 作者:行者123 更新时间:2023-12-01 19:31:10 25 4
gpt4 key购买 nike

我在运行bundle exec rake 测试时遇到失败。有问题的文件是 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 测试,我得到以下“失败”:

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?" } %>

最佳答案

也许问题出在您的模板中:当您调用 render @users 时,Rails 已经遍历了 @users 处的用户列表,因此您不需要需要 .each do 部分:

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

你可以简单地这样做:

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

我是遵循相同教程的新手,所以不确定这是否是一个好的答案。如果有帮助请告诉我...

关于ruby-on-rails - Ruby on Rails 4 测试失败(删除功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27453178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com