水豚,rspec出现“未定义的方法访问”错误

时间:2018-07-03 07:47:03

标签: ruby-on-rails rspec capybara

我正在尝试将rspeccapybara一起使用。

Gemfile

group :test do
  gem 'capybara'
end

根据文档,我需要向{em> rails_helper.rb 中添加require 'capybara/rspec'

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require 'rspec/rails'
require 'capybara/rspec'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!    
  config.filter_rails_from_backtrace!
end

现在,当我运行bundle exec rspec时。我得到:

Failure/Error: visit('/')

     NoMethodError:
       undefined method `visit' for #<RSpec::ExampleGroups::HomeFooters:0x000000035cc4b8>

测试如下:

require 'rails_helper'

RSpec.describe "HomeFooters", type: :request do
  it "show proper links in footer" do 
    visit('/')
    page.should have_content("Jobs")
  end
end

我该如何解决此错误?

感谢您的帮助!谢谢!

1 个答案:

答案 0 :(得分:4)

capybara不能与type: :request一起使用,而是type: :feature