2

I am following the tutorial from Michael Hartl . I tried the first test example for testing the app with Rspec and when I execute this command "bundle exec rspec spec\requests\static_pages_spec.rb" I get this error.

F

Failures:

  1) Home page should have the content 'Sample App'
     Failure/Error: visit '/static_pages/home'
     NoMethodError:
       undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x39e1510 @example=nil>
     # ./spec/requests/static_pages_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 0.001 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:3 # Home page should have the content 'Sample App'

static_pages_spec.rb

describe "Home page" do
  it "should have the content 'Sample App'" do
    visit '/static_pages/home'
    page.should have_content('Sample App')
 end
end

Gemfile.rb

source 'https://rubygems.org'

gem 'rails', '3.2.1'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.10.0'
end

group :assets do
  gem 'sass-rails', '3.2.4'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails'

group :test do
  gem 'capybara', '1.1.2'
end

group :production do
  gem 'pg', '0.12.2'
end
5
  • @ronalchn Check out the entire error message. Commented Sep 4, 2012 at 0:44
  • Please see visit method not found in my spec. Commented Sep 4, 2012 at 0:48
  • The problem is not with content of the page . 'visit' is not recognized. @Dave - For visit to work you need the capybara and that's installed . Don't know why this is still happening Commented Sep 4, 2012 at 1:08
  • @Dave - Thanks . I read it fully and it says By default the capybara DSL is included automatically if the file is in spec/requests , so since my static_pages_spec.rb is inside spec/requests it should'nt be a problem . Please advice . Commented Sep 4, 2012 at 1:36
  • See this discussion: github.com/rspec/rspec-rails/issues/360 might be relevant, but depends on what's in spec_helper.rb. Commented Sep 4, 2012 at 1:52

2 Answers 2

4

You need to require 'spec_helper' in your spec source.

Your spec_helper should include both rspec/rails and capybara/rails in it.

You'll want to use get instead of visit if you want to access the response, however.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow ! Got it working . @Dave : Thanks a lot. Did exactly the above things and it worked . T
1

If static_pages_spec.rb has string require 'spec_helper' and you get

Failure/Error: visit '/static_pages/home'

  • add to spec_helper.rb string config.include Capybara::DSL

it helped me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.