7
votes

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.

3
Are you using Capybara 2.0?Jesse Wolgamott
Yes I was! My google-search-fu was weak.Jinyoung Kim

3 Answers

13
votes

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.
3
votes

Just some additional info for anybody having the same problem with the Capybara upgrade to 2.x. Check out rspec-rails docs under the Upgrading to Capybara 2 section.

Basically, In order to use the Capybara DSL(page & visit) you must move your existing specs into the spec/features directory. So you can only use page & visit in acceptance tests. No more page & visit in controller and request specs. Only the rack-test DSL (get|post|put|delete|head/response.body) is allowed in controller and request specs.

This is not recommended but there is a way to keep your specs working as they are:

RSpec.configure do |c|
  c.include Capybara::DSL, :example_group => {
    :file_path => "spec/requests"
  }
end

The docs state that if you go this route then you are overriding the intended behavior and you are taking a risk.

And definitely don't make this as a reason not to upgrade to Capybara 2.x. Feature specs are easy to get used to and are easy to read. feature is just an alias for describe, background is an alias for before, scenario for it, and given for let.

Hope this helps anyone confused by the new changes.

-3
votes

The issue is in capybara gem itself.

gem 'capybara', '1.1.2' solves this issue ( Version 2.0.x fails )