安装水豚后控制器测试损坏? [英] Broken controller tests after installing Capybara?
问题描述
我用rspec编写了一堆组合的控制器/视图测试。我添加了水豚宝石,并编写了一些集成测试,这些测试通过得很好。唯一的问题是,现在在我所有的控制器测试中,我都有
I had a bunch of combined controller/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have
response.should have_selector( some selector)
response.should have_selector("some selector")
rspec给出以下错误:
rspec gives errors such as:
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec>
当我运行控制器测试时。我猜测控制器测试中正在使用Capybara,并且已经覆盖了一些Rspec方法。我该如何解决?
when I run controller tests. I'm guessing that Capybara is being used in my controller tests and has overwritten some Rspec methods. How can I fix this?
# gemfile.rb
group :test do
gem 'rspec'
gem "capybara"
gem "launchy"
gem 'factory_girl_rails', '1.0'
end
# spec_helper.rb
RSpec.configure do |config|
config.include IntegrationSpecHelper, :type => :request
end
以下是测试失败的示例:
Here's an example of a failing test:
# spec/controllers/books_controller_spec.rb
require 'spec_helper'
describe BooksController do
render_views
it "should have the right page title" do
get :show, :id => @book.ean
response.should have_selector("title", :content => "Lexicase | " + @book.title)
end
end
及其相关错误:
1) BooksController GET 'show' should have the right page title
Failure/Error: response.should have_selector("title", :content => "Lexicase | " + @book.title)
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa8488c0>
# ./spec/controllers/books_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
推荐答案
您以前可能使用过Webrat,而 has_selector?是Webrat的匹配器。 Capybaras没有has_selector匹配器,它有一个名为 has_css 的匹配器。您可能要用 has_css替换 has_selector。
You were probably using Webrat earlier, and has_selector? is a Webrat matcher. Capybaras doesn't have a has_selector matcher, it has a matcher called has_css. You may want to replace the "has_selector" with "has_css".
这篇关于安装水豚后控制器测试损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!