Search code examples
ruby-on-railsrspeccapybara

Why is capybara not available in request specs?


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.


Solution

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