在使用Ruby 2一个Rails应用程序3.2.14使用RSpec的护栏2.14.0和水豚2.1.0以下特点规范导致了失败:水豚/ RSpec的“have_css”匹配不工作,但has_css确实
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.
title元素和正确的文本是所呈现的页面
上但是,当我改变这一行的功能规格:
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
任何人都知道我可能是做错了什么?
请继续前进,看到这篇文章,尤其是水豚部分http://nofail.de/2013/10/debugging -rails-applications-in-development/ – phoet
如果你只是做了'page.has_css?('title',text:'Todo')',测试总是会通过。 'has_css?'只是返回true或false,这不足以使测试失败。如果你输出它,你可能会看到它是错误的。你确定页面有一个标题元素和文本? –
@JustinKo感谢您指出新手错误。是的,标题显示了适当的文字 - 在浏览器中检查。 –