54

My java web application is running on tomcat at http://localhost:8080/

Writing my first spec, home_spec:

require 'spec_helper'


describe "home" do

    it "should render the home page" do
       visit "/"

       page.should have_content("hello world")
    end

end

And running:

rspec

I get:

F

Failures:

  1) home should render the home page
     Failure/Error: visit "/"
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
     # ./spec/home/home_spec.rb:7:in `(root)'

Finished in 0.012 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/home/home_spec.rb:6 # home should render the home page

Shouldn't this work because I have included capybara in the spec_helper?

How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?

My gemfile:

source 'http://rubygems.org'

gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"

My spec_helper:

require 'capybara/rspec'
Blankman
  • 259,732
  • 324
  • 769
  • 1,199

6 Answers6

135

Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)

you should put

config.include Capybara::DSL

in spec_helper.rb, inside the config block.

Erdem Gezer
  • 3,142
  • 2
  • 22
  • 16
23

The default directory that Capybara::RSpec now looks at to include the Capybara::DSL and Capybara::RSpecMatchers is changed from requests to features.

After I renamed my requests directory to features I got the matcher and DSL methods available again without having to explicitly include them.

See the following commit

steve.clarke
  • 355
  • 3
  • 7
  • 1
    also rename the "describe" to "feature" – Marta Kostova Apr 30 '13 at 23:13
  • Great explanation.Thank you. But instead of changing the directory name, I just followed @egezer's solution. Both works. – PriyankaK Jul 19 '13 at 14:23
  • Renaming (moving tests from) spec/requests to spec/features is recommended. See: http://www.andylindeman.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html – Arta Jan 22 '14 at 22:51
  • The rspec-rails docs have good tips on the default location for each type of test: https://github.com/rspec/rspec-rails – Jon Kern Feb 22 '14 at 16:49
  • Let me add: Both `requests` and `features` are allowable directories, but for different purposes. Though renaming your directory *will* work, you may want to ensure the type of spec you are writing is in the proper place. That is, if you want to use `visit` (aka, capybara stuff), then put those specs in a features directory, and leave request-style specs in the `requests` directory. – Jon Kern Feb 22 '14 at 16:57
9

Also make sure your tests are in the /spec/features directory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."

Chris Hough
  • 3,389
  • 3
  • 41
  • 80
shadowbrush
  • 141
  • 2
  • 2
6

By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.

Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSL should also do the trick (you might also need to replicate some of the before(:each) stuff that would be setup.)

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • I renamed spec/home/home_spec.rb to spec/requests/home_spec.rb and it didn't change anything. adding the include I now get: `rack-test requires a rack application, but none was given` – Blankman Jan 14 '12 at 15:23
  • should I put a folder for support inside the requests folder? i.e. /spec/requests/support/env.rb ? – Blankman Jan 14 '12 at 15:36
  • Ah, I'd missed that you weren't testing a rails app. You'd need at the very least to set the capybara driver to selenium, but other than that I'm not sure – Frederick Cheung Jan 14 '12 at 16:26
  • does it automatically load up spec/requests/support/env.rb or I have to do this somehow? or should I put it in /spec/support/env.rb ?? – Blankman Jan 14 '12 at 18:17
1

First check it out

If you are not success,

Add this code your end of the your spec helper actually out of the RSpec.configure block as well

module ::RSpec::Core
  class ExampleGroup
    include Capybara::DSL
    include Capybara::RSpecMatchers
  end
end
eayurt
  • 1,169
  • 1
  • 19
  • 42
-1

1) Add to ‘rails_helper’ config:

config.include Capybara::DSL
config.include Capybara::RSpecMatchers

And comment out the `require 'spec_helper'` line.

2) Add to 'spec_helper':

require 'rails_helper'
Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53