在带有 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
有谁知道我可能做错了什么?