问题描述
在具有Ruby 2的Rails 3.2.14应用程序中,使用rspec-rails 2.14.0和capybara 2.1.0,以下功能规范导致失败:
In a Rails 3.2.14 app with Ruby 2, using rspec-rails 2.14.0 and capybara 2.1.0 the following feature spec is causing a failure:
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.
标题元素和正确的文本是
The title element and correct text are on the rendered page
但是当我在功能说明中更改此行时:
But when I change this line in the feature spec:
expect(page).to have_css('title', text: "Todo")
对此:
page.has_css?('title', text: "Todo")
然后测试通过。
如何获取 have_css(...)
表格可以工作吗?是配置问题吗?
How do I get the have_css(...)
form to work? Is it a configuration issue?
这是我的 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
有人知道我可能做错了吗?
Anyone know what I might be doing wrong?
推荐答案
默认情况下,水豚只查找可见元素。 head元素(及其标题元素)并不是真正可见的。
By default, Capybara only looks for "visible" elements. The head element (and its title element) are not really considered visible. This results in the title element being ignored in have_css.
您可以通过:visible =>强制Capybara也考虑不可见元素。 false
选项。
expect(page).to have_css('title', :text => 'Todo', :visible => false)
但是,使用 have_title
方法:
expect(page).to have_title('Todo')
这篇关于Capybara / RSpec'have_css'匹配器不起作用,但是has_css起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!