36

When running my specs with rspec & capybara, it can't find capybara's visit method. Is there another initialization step I need to do?

$bundle exec rspec spec
/home/brian/projects/expense_track/expense_track/spec/requests/homepage_spec.rb:6:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xb6572b8> (NoMethodError)

Gemfile:

group :test, :development do
  gem "rspec-rails"
  gem "capybara"
end

top of my spec_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)

require 'rspec/rails'
require 'capybara/rspec'
require 'rspec/autorun'

homepage_spec.rb:

require 'spec_helper'

describe "The home page" do

  context "home page exists" do
    visit "/"
    page.should have_content("elephants")
  end
end
bwbrowning
  • 6,200
  • 7
  • 31
  • 36
  • 1
    figured it out, my spec was borked. Need an actual rpec example, it "should have a home page that exists" do... – bwbrowning Jan 30 '12 at 06:19
  • I am marking duplicate of https://stackoverflow.com/questions/15148585/undefined-method-visit-when-using-rspec-and-capybara-in-rails ; please note that although this post is older, the other one has a higher Google organic. I believe the Google Organic should determine which SO post gets marked as the "winner" as this is the most logical and effective way to use SO. @Martijn Pieters -- please meet me face to face in the admin forums if you disagree. – Jason FB Feb 14 '22 at 12:11
  • Does this answer your question? [undefined method \`visit' when using RSpec and Capybara in rails](https://stackoverflow.com/questions/15148585/undefined-method-visit-when-using-rspec-and-capybara-in-rails) – Jason FB Feb 14 '22 at 12:13

6 Answers6

50

Just ran into this issue myself.

So the reason for this is there has been a somewhat undocumented change in Capybara. Capybara now makes the assumption that anything using it needs to be in the spec/features folder and it will make the proper assumptions. Anything left in the spec/requests folder will no longer work. Though there are workarounds.

For a context block you can add the parameter :type => :feature and this will fix that issue or you can change the name of a describe method at the beginning of a spec to feature and this should change it as well.

They announced this change in their Google group: https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/5KfxezI-U0Q

Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.

This was further discussed in the github issue: https://github.com/jnicklas/capybara/issues/814

Eric C
  • 2,195
  • 1
  • 17
  • 23
  • 5
    This should have been trumpeted from the rafters - I just wasted a morning on it. – tentimes Nov 27 '13 at 10:32
  • best to use `config.infer_spec_type_from_file_location!` – Jason FB Feb 14 '22 at 12:05
  • there are two other reasons 1) You for got to put the visit inside of an `it` block, 2) You have a kernal panic due to some bad Ruby and rspec shut the spec down. – Jason FB Feb 14 '22 at 12:05
19

A few things to note here :

  1. The changes in Capybara 2.0.x are documented here https://github.com/rspec/rspec-rails/blob/master/Capybara.md . There are changes in the spec directory structure : spec/features, spec/controllers, spec/views, spec/helpers, spec/mailers.
  2. load Capybara dsl explicitly inside your spec_helper


       require 'capybara/rails'
       require 'capybara/rspec'
       include Capybara::DSL
Saurabh Bhatia
  • 337
  • 3
  • 8
  • This "worked" for me, but it's not what I wanted. Instead of the normal phrasing, e.g. `expected to find text "My albums" in ...` (as when you use the `features` directory as noted in another answer), I got this: `expected #has_content?("My albums") to return true, got false`. It's clear enough, but not ideal. – Tyler Collier Jun 11 '13 at 05:21
  • include Capybara::DSL This worked when placing my login steps within a totally separate module within step_definitions. – etusm Mar 19 '15 at 19:34
  • best to use `config.infer_spec_type_from_file_location!` – Jason FB Feb 14 '22 at 12:04
8

This worked for me.

require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'

RSpec.configure do |config|
  config.include Capybara::DSL, :type => :request
end

This enables you to use Capybara's helpers inside spec/requests.

Ananth
  • 837
  • 1
  • 10
  • 20
  • 1
    More generally, you can add the DSL without a specific type, like so: `config.include Capybara::DSL` (instead of `config.include Capybara::DSL, :type => :request`) to have the DSL in all test files. I haven't looked into the code itself, but when I said `:type => :feature`, Capybara's DSL did not in fact load into my `spec\features\*rb` files. – sameers Dec 31 '14 at 20:03
  • best to use `config.infer_spec_type_from_file_location!` – Jason FB Feb 14 '22 at 12:04
5

Because RSpec.configure not including capybara DSL in spec_helper.rb

It is an ugly solution, but you can add this to your spec_helper.rb.

module ::RSpec::Core
  class ExampleGroup
    include Capybara::DSL
    include Capybara::RSpecMatchers
  end
end 

The git issue for this:

https://github.com/rspec/rspec-rails/issues/503

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
jwall
  • 759
  • 1
  • 8
  • 17
  • Wrong bet, the topic has been closed on the git issue you provided. So the answer will not work. – citraL Sep 26 '12 at 14:38
2

Unfortunately this workaround doesn't do it for me. I still get

NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>

The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter You need to look at the branch '28817499-subscribe'

edit: If i put include Capybara::DSL inside my describe block it works.

but including Capybara::DSL in the global scope is not recommended!

Because I do not know a good way.

Leon Xu
  • 29
  • 5
0

For rspec 3 and rails, make sure you are using require "rails_helper", instead of require "spec_helper".

Otherwise, review the latest changes to rspec 3 & rspec-rails and Capybara 2.0.x.

Matt Scilipoti
  • 1,091
  • 2
  • 11
  • 15