安装Capybara后控制器测试是否破损?

时间:2011-03-22 08:40:22

标签: ruby-on-rails ruby-on-rails-3 rspec capybara

我有一堆用rspec编写的组合控制器/视图测试。我添加了Capybara gem并编写了一些完整的集成测试。唯一的问题是,现在我所有的控制器测试中都有

response.should have_selector(“some selector”)

rspec会出现以下错误:

NoMethodError:
       undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec>

当我运行控制器测试时。我猜测Capybara正在我的控制器测试中使用,并且已经覆盖了一些Rspec方法。我该如何解决这个问题?

# 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

以下是失败测试的示例:

# 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)>'

2 个答案:

答案 0 :(得分:2)

您之前可能正在使用Webrat,而 has_selector?是Webrat匹配器。 Capybaras没有has_selector匹配器,它有一个名为 has_css 的匹配器。您可能希望将“has_selector”替换为“has_css”。

答案 1 :(得分:1)

Capybara帮助程序仅适用于请求规范。要么创建新的请求规范,要么传入:type =&gt; :描述块部分中的请求,如下所示:

describe "test for the testing test", :type => :request do
  it "should work with capybara" do
    visit root_path
    click_link "Home"
    page.should WHATEVA
  end
end

我很久以前就意识到这个问题,但我想我还是会分享。 GLHF