Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
Jinyoung Kim Avatar asked Dec 06 '12 16:12

Jinyoung Kim


People also ask

What do you learn in the capybara series?

In the final article of this two part series (see the other article here) you'll gain an in-depth understanding of controller specs, request specs and feature specs. You'll also learn how to use Capybara to describe a typical user story where all interactions are driven via the user interface.

What is a capybara user story?

You'll also learn how to use Capybara to describe a typical user story where all interactions are driven via the user interface. For example, when a user visits a home page, gets redirected to sign in, fills the login form and clicks the login button.

Should I write a Controller test or a request spec?

“The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses.


1 Answers

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.
like image 158
Jinyoung Kim Avatar answered Sep 22 '22 21:09

Jinyoung Kim