gpt4 book ai didi

rspec - Capybara/RSpec 'have_css' 匹配器不起作用,但 has_css 起作用

转载 作者:行者123 更新时间:2023-12-04 01:54:25 24 4
gpt4 key购买 nike

在带有 Ruby 2 的 Rails 3.2.14 应用程序中,使用 rspec-rails 2.14.0 和 capybara 2.1.0 以下功能规范导致失败:

require 'spec_helper'

feature 'View the homepage' do
scenario 'user sees relevant page title' do
visit root_path
expect(page).to have_css('title', text: "Todo")
end
end

失败信息是:
 1) View the homepage user sees relevant page title
Failure/Error: expect(page).to have_css('title', text: "Todo")
Capybara::ExpectationNotMet:
expected to find css "title" with text "Todo" but there were no matches. Also
found "", which matched the selector but not all filters.

标题元素和正确的文本位于呈现的页面

但是当我在功能规范中更改这一行时:
expect(page).to have_css('title', text: "Todo")

对此:
page.has_css?('title', text: "Todo")

然后测试通过。 [编辑 - 但请参阅下面来自@JustinKo 的回复,该测试不是一个好的测试,因为它总是会通过的]

我如何获得 have_css(...)表格工作?是配置问题吗?

这是我的 Gemfile 的相关部分:
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end

还有我的 spec/spec_helper.rb是这样设置的:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|

# out of the box rspec config code ommitted

config.include Capybara::DSL
end

有谁知道我可能做错了什么?

最佳答案

默认情况下,Capybara 只查找“可见”元素。 head 元素(及其标题元素)实际上并不被认为是可见的。这会导致在 have_css 中忽略标题元素。

您可以通过 :visible => false 强制 Capybara 也考虑不可见元素。选项。

expect(page).to have_css('title', :text => 'Todo', :visible => false)

但是,使用 have_title 会更容易。方法:
expect(page).to have_title('Todo')

关于rspec - Capybara/RSpec 'have_css' 匹配器不起作用,但 has_css 起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19519844/

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