breaking into the unknown…

undefined method `visit’ for rspec capybara


Recently, I am trying to integrate rspec and capybara with sinatra (see here). But when I try to run my test file, I get below error

undefined method `visit’ for #<RSpec::Core::ExampleGroup::Nested_1:0x9209d5c @example=nil>

I find it strange, as I have configured capybara many a time with rails and it work smoothly. I get some hint of problem here on stack overflow.

visit is a method defined by capybara over rspec layer i,e it is DSL (domain specific language). when working with rails, capybara DSL module is automatically loaded by rails when rspec is loaded, but with sinatra, this is not happening. So we have to include  Capybara::DSL in the file we want to use its method. I have added the include line in my spec_helper.rb file which is required in my all other test files.

SOLUTION : add include Capybara::DSL to your configuration file or the individual test file

My, spec_helper.rb file look like this

require 'capybara'
include Capybara::DSL # Adding this line solved the error
Capybara.default_driver = :selenium

def app 

set :environment, :test 
RSpec.configure do |config| 
  config.treat_symbols_as_metadata_keys_with_true_values = true 
  config.run_all_when_everything_filtered = true 
  config.filter_run :focus config.order = 'random' 

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

3 thoughts on “undefined method `visit’ for rspec capybara

  1. I used that method at first until I looked closer at the Capybara source code and realized that the default location for rspec integration tests has changed.

    I had my integration tests under the spec/requests directory as I’ve seen it done over the years.

    Now it seems to require being named spec/features in order for the Capybara::DSL methods to be included automatically.

    Check your directory naming and you just might be able to solve this problem much easier.

    • Hi clarke,

      glad to see your input.
      I again want to point out that the solution for above error is to add include Capybara::DSL in the file throwing the error.Now, it may be included explicitly as I have mentioned above in the post or included implicitly if you are following some convention.

      If we see the github code of the file here It look like this

      RSpec.configure do |config|
      config.include Capybara::DSL, :type => :feature
      config.include Capybara::RSpecMatchers, :type => :feature

      So, If you write your test within feature end block instead of describe end block the capybara dsl will be added automatically

      feature "users loan documents" do
      visit root_url #visit will work here as Capybara::DSL included automatically

      describe "users loan documents" do
      visit root_url #visit will not work here. you need to include Capybara::DSL yourself

      You can follow more on this issue on github itself. Some good insight is also available here

  2. awesome – took me forever to fix this. both worked, but I chose modifying the directory structure. Thank you!!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s