gpt4 book ai didi

ruby-on-rails - assert_select 在测试中不起作用

转载 作者:行者123 更新时间:2023-11-28 19:58:01 25 4
gpt4 key购买 nike

我在互联网上搜索了答案,但一无所获...这让我发疯!

没有assert_select 语句在我的任何测试中起作用。我已经反复确认该元素实际上位于目标页面的 dom 中——确实如此——但每个测试都在 assert_select 语句上失败。甚至像 assert_select 'body' 这样通用!

这是一个测试:

require 'test_helper'

class UsersIndexTest < ActionDispatch::IntegrationTest
def setup
@user = users(:matt)
end

test "index including pagination" do
log_in_as(@user)
get users_path
assert_template 'users/index'
assert_select 'div.pagination'
User.paginate(page: 1).each do |user|
assert_select 'a[href=?]', user_path(user), text: user.first_name
end
end
end

这里是users/index.html.haml

%h1 All Users

= link_to 'New User', new_user_path

= will_paginate

%table
%thead
%tr
%th
%th First name
%th Last name
%th Email
%th
%th
%th

%tbody
- @users.each do |user|
%tr
%td= gravatar_for(user)
%td= user.first_name
%td= user.last_name
%td= user.email
%td= link_to 'Show', user
%td= link_to 'Edit', edit_user_path(user)
%td= link_to 'Destroy', user, :method => :delete, :data => { :confirm => 'Are you sure?' }

= will_paginate

很无聊的东西,对吧?然而,测试失败:

FAIL["test_index_including_pagination", UsersIndexTest, 0.9574]
test_index_including_pagination#UsersIndexTest (0.96s)
Expected at least 1 element matching "div.pagination", found 0..
Expected 0 to be >= 1.
test/integration/users_index_test.rb:12:in `block in <class:UsersIndexTest>'

即使我将断言更改为 assert_select div,它也会失败。而且,当我访问索引页面时,我可以再次确认 div 存在。

我在这里错过了什么!?!?!?!

如果有帮助,这是我的 gemfile:

    source 'https://rubygems.org'

gem 'rails', '4.2.0'
gem 'pg'
gem 'puma'
gem 'turbolinks'
gem 'jquery-rails'
gem 'bcrypt', '~> 3.1.7'
gem 'faker', '~> 1.4.2'
gem 'sass-rails', '~> 5.0.1'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'will_paginate', '~>3.0.7'
gem 'bootstrap-will_paginate','~>0.0.10'
gem 'uglifier', '>= 2.5.0'
gem 'haml-rails', '~> 0.8'
gem 'jbuilder', '~> 2.2.3'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'rest-client', '~> 1.7.3'
gem 'bourbon'

group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0.0'
gem 'spring'
gem 'better_errors'
end

group :test do
gem 'minitest-reporters', '~> 1.0.5'
gem 'mini_backtrace', '~> 0.1.3'
gem 'guard-minitest', '~> 2.3.1'
end

group :production do
gem 'rails_12factor'
end

ruby '2.2.0'

感谢 Steve Klein 提出打印响应正文的建议。不幸的是,情节只会变厚。身上明显有我要瞄准的标签!

在上面的测试中添加了一行

assert_template 'users/index'
puts @response.body
assert_select 'div.pagination'

并且输出有标签!

← 上一个 1

  • 2
  • 下一个 →

    但还是失败了!!!

    FAIL["test_index_as_admin_including_pagination_and_delete_links", UsersIndexTest, 1.412393] test_index_as_admin_including_pagination_and_delete_links#UsersIndexTest (1.41s)
    Expected at least 1 element matching "div.pagination", found 0..
    Expected 0 to be >= 1.
    test/integration/users_index_test.rb:15:in `block in <class:UsersIndexTest>'

    最佳答案

    确保您正在加载足够的测试数据来触发分页。除非分页启动,否则此选择器不会出现在您的页面上(取决于您如何在 Controller 操作中配置 will_paginate)。

    关于ruby-on-rails - assert_select 在测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641951/

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