安装 Capybara 后控制器测试损坏? [英] Broken controller tests after installing Capybara?

查看:18
本文介绍了安装 Capybara 后控制器测试损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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".

这篇关于安装 Capybara 后控制器测试损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆