简体   繁体   English

NoMethodError:#的未定义方法“访问”

[英]NoMethodError: undefined method `visit' for #<RSpec::

I'm following the Ruby on Rails tutorial and ran into the following problem: 我正在关注Ruby on Rails教程,并遇到以下问题:

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

F F

Failures: 失败:

1) Static pages Home page should have the content 'Sample App' Failure/Error: visit '/static_pages/home' 1)静态页面主页应包含“示例应用程序”内容失败/错误:访问“ / 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 在0.0006秒内完成(文件加载时间为0.08149秒)1例,1例失败

Failed examples: 失败的例子:

rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the content 'Sample App' rspec ./spec/requests/static_pages_spec.rb:7#静态页面首页应具有内容“ Sample App”

Here is my Gemfile: 这是我的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: 我的规格/要求/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. 在线挖掘建议将config.include Capybara :: DSL放在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. 另一个建议是说,我需要将static_pages_spec.rb移至规范/功能。 I did that and that didn't work either. 我做到了,那也不起作用。

Help? 救命? :D :D

visit is a method used in feature specs. visit是功能规格中使用的一种方法。 In your request specs you instead use the HTTP verbs get , post , patch etc to send http requests. 在您的请求规范中,您改为使用HTTP动词getpostpatch等来发送http请求。

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 . 我相信您使用的主题以及教程都已经过时了subject.should已经折旧了很长时间,转而使用了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. 当您遇到麻烦时,您可能需要参考官方的RSpec-rails文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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