我正在使用rspec来测试我的应用,但是我无法使用visit方法访问我的root_path。我尝试添加rails-controller-testing gem,但它无济于事。使用Rails 5.1.1,Ruby 2.4.1,rspec 3.6.0。
这是我的错误:
$ rspec
F
Failures:
1) navigate homepage can be reached successfully
Failure/Error: visit root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::ExampleGroups::Navigate::Homepage:0x007fbf3c224748>
# ./spec/features/static_spec.rb:5:in `block (3 levels) in <top (required)>'
Finished in 0.00378 seconds (files took 0.09639 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/static_spec.rb:4 # navigate homepage can be reached successfully
static_spec.rb文件:
#spec/features/static_spec.rb
require 'spec_helper'
describe 'navigate' do
describe 'homepage' do
it 'can be reached successfully' do
visit root_path
expect(page.status_code).to eq(200)
end
end
end
我已经尝试在spec_helper.rb
中使用它config.include rails.application.routes.url_helpers
这是我的路线。
#config/routes.rb
Rails.application.routes.draw do
root :to => 'static#homepage'
end
这是rails_helper.rb
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 'spec_helper'
require 'rspec/rails'
require 'capybara/rails'
include Warden::Test::Helpers
Warden.test_mode!
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.before(:suite) { DatabaseCleaner.clean_with(:truncation) }
config.before(:each) { DatabaseCleaner.strategy = :transaction }
config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation }
config.before(:each) { DatabaseCleaner.start }
config.after(:each) { DatabaseCleaner.clean }
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.include FactoryGirl::Syntax::Methods
end
如何解决此错误?