undefined method visit for RSpec
Contact me at LinkedIn  RailsZilla at Facebook  RailsZilla at twitter   google +1  Contact me at Xing  connect me at github

undefined method visit for RSpec

Posted in Rails, Ruby

Writing full-stack tests is important. In this article it’s all about behavior-driven development (abbreviated BDD) and fixing the errors which can occour. I have three simple steps to check that your test will run.

Lets say we use the following Integration Test in our static_pages_spec.rb:

1
2
3
4
5
6
7
8
9
require 'spec_helper'
describe "Static pages" do
  describe "Home page" do
    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      page.should have_content('Sample App')
    end
  end
end

Many a time, friends from me got this error:

1
undefined method `visit' for #<RSpec

This can fixed very easy, read on …

1. The first thing to know is, ‘visit’ is a capybara method and not from Rspec.
Be sure to have all necessary gems installed in your Gemfile. These gems are essential for our example with BDD:

1
2
3
4
group :test, :development do
  gem "rspec-rails", "~> 2.0"
  gem 'capybara', '~>1.1.2'
end

2. Capybara is not included in views spec.
It is for integration testing. Now try to move your spec file into “spec/requests” directory.

3. Include Capybara domain-specific language (DSL) in your helper.
Go to the file “spec/spec_helper.rb” and add the following to that file:

1
config.include Capybara::DSL

This must be in your RSpec.configure blog, so that the file should something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.include Capybara::DSL


  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

You can copy & paste it, if you do not have special configurations. These three steps should fix your error.
Have fun.

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enter a secure code to see if you are a spammer ;-)

 

Copyright © 2011-2017  - RailsZilla – Ruby on Rails tutorials, tips and tricks All rights reserved. | Imprint | Privacy