【问题标题】:NoMethodError: undefined method `visit' for #<RSpec::NoMethodError:#<RSpec:: 的未定义方法“访问”
【发布时间】:2016-05-25 19:52:33
【问题描述】:

我正在关注 Ruby on Rails 教程并遇到以下问题:

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

F

失败:

1) 静态页面首页应该有内容'Sample App' 失败/错误:访问'/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)>'

在 0.0006 秒内完成(加载文件需要 0.08149 秒) 1 个示例,1 个失败

失败的例子:

rspec ./spec/requests/static_pages_spec.rb:7 # 静态页面首页应该有内容'Sample App'

这是我的 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

我的规范/请求/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

在网上搜索建议将 config.include Capybara::DSL 放在 spec/spec_helper.rb 中。我这样做了,但没有工作。另一个建议说我需要将我的 static_pages_spec.rb 移动到规范/功能。我这样做了,但也没有用。

帮助? :D

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    visit 是功能规范中使用的一种方法。 在您的请求规范中,您改为使用 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
    

    我相信你使用的是一个严重过时的教程,并且subject.should 已经贬值了很长时间,有利于 expect(subject).to.

    虽然您可以将 capybara 混合到您的请求规范中,但最好将请求规范用作低级别的集成测试,并将更高级别的点击和按钮推送留给您的功能规范。

    当您遇到问题时,您可能需要参考官方的 RSpec-rails 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-26
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2016-03-13
      • 2018-04-20
      相关资源
      最近更新 更多