简体   繁体   中英

NoMethodError: undefined method `visit' for #<RSpec::

I'm following the Ruby on Rails tutorial and ran into the following problem:

me@me:~/Ruby/sample_app$ bundle exec rspec spec/requests/static_pages_spec.rb

F

Failures:

1) Static pages Home page should have the content 'Sample App' Failure/Error: visit '/static_pages/home'

 NoMethodError:
   undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x00000001e1df88>
 # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.0006 seconds (files took 0.08149 seconds to load) 1 example, 1 failure

Failed examples:

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

Here is my Gemfile:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'

group :development, :test do
  # Use sqlite3 as the database for Active Record
  gem 'sqlite3'

  gem 'rspec-rails'
end

group :assets do
  # Use SCSS for stylesheets
  gem 'sass-rails', '~> 5.0'
  # Use Uglifier as compressor for JavaScript assets
  gem 'uglifier', '>= 1.3.0'
  # Use CoffeeScript for .coffee assets and views
  gem 'coffee-rails', '~> 4.1.0'
end


# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

group :test do
  gem 'capybara'
end

group :prodcution do
  gem 'pg'
end

My spec/requests/static_pages_spec.rb:

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

Digging up online suggested putting config.include Capybara::DSL in spec/spec_helper.rb. I did that and didn't work. Another suggestion said I need to move my static_pages_spec.rb to spec/features. I did that and that didn't work either.

Help? :D

visit is a method used in feature specs. In your request specs you instead use the HTTP verbs get , post , patch etc to send http requests.

require 'rails_helper'

RSpec.describe "Static pages" do
  describe "Home page" do
    it "should have the content 'Sample App'" do
      get '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end
end

I believe you are using a badly outdated tutorial as well as subject.should has been depreciated for a long time in favor of expect(subject).to .

While you can mix capybara into your request specs it may be better to use request specs as a low level integration tests and leave the higher level clicking and button pushing to your feature specs.

When you run into trouble you might want to refer to the official RSpec-rails documentation instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM