为什么水豚在请求规格中不可用? [英] Why is capybara not available in request specs?
问题描述
使用rspec和capybara开发新的Rails 3.2.9应用。
我在Gemfile中具有以下内容:
宝石'rspec-rails'
宝石'水豚'
以及spec / spec_helper.rb中的以下内容:
需要'rspec / rails'
需要'capybara / rspec'
并在spec / requests / asdf_spec.rb中:
需要'spec_helper'
描述'Asdf'做
描述 GET / asdfs做
应该列出asdfs做
访问asdfs_path
结束
结束
结束
此测试失败:
失败/错误:访问asdfs_path
$ p $中的(4个级别) p>
NoMethodError:
#< RSpec :: Core :: ExampleGroup :: Nested_1 :: Nested_2 :: Nested_1:0x007fa7b68961a0>的未定义方法`visit';
#./spec/requests/asdfs_spec.rb:19:in`top(必需)>'
所以看起来水豚没有被装上。阿克,为什么不呢?我觉得我之前已经做过十多次相同的事情了……可能是在愚蠢的事情上做空了。
解决方案所以这是水豚第2版的更改。我发现了这一点:
http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need- to-know.html
这说明:
Upon升级到capybara 2.0后,在RSpec请求规范中,默认
不能使用 capybara 。而是创建了一种新的规格-
功能规格-供水豚使用。
要升级到水豚2.0,您将需要做一些事情:
- 将rspec-rails升级到2.12.0或更高版本
- 将使用水豚的所有测试从规格/要求移至规格/功能。 Capybara
测试使用visit方法,通常针对页面进行断言。
Working on a new Rails 3.2.9 app with rspec and capybara.
I have the following in the Gemfile:
gem 'rspec-rails' gem 'capybara'
and the following in spec/spec_helper.rb:
require 'rspec/rails' require 'capybara/rspec'
and in spec/requests/asdf_spec.rb:
require 'spec_helper' describe 'Asdf' do describe "GET /asdfs" do it "should list asdfs" do visit asdfs_path end end end
This test is failing:
Failure/Error: visit asdfs_path NoMethodError: undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x007fa7b68961a0> # ./spec/requests/asdfs_spec.rb:19:in `block (4 levels) in <top (required)>'
So it looks like Capybara isn't getting loaded. Ack, why not? I feel like I've done this exact same thing a dozen times before... probably blanking on something stupid.
解决方案So it was a capybara version 2 change. I found this:
http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
which explains:
Upon upgrading to capybara 2.0, capybara will not be available by default in RSpec request specs. Instead, a new type of spec--the feature spec--has been created for use with capybara.
To upgrade to capybara 2.0, you'll need to do a few things:
- Upgrade rspec-rails to 2.12.0 or greater
- Move any tests that use capybara from spec/requests to spec/features. Capybara tests use the visit method and usually assert against page.
这篇关于为什么水豚在请求规格中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!