我对rails非常陌生,并试图遵循railstutorial。一切都很顺利,除了我的测试无法超越命名路线(5.3.3)
我的routes.rb:
SampleApp::Application.routes.draw do
resources :users
match '/signup', to: 'users#new'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'pages#contact'
root to: 'static_pages#home'
#Commented stuff
我的第一次测试(spec / controllers / static_pages_controller_spec.rb):
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading) }
it { should have_selector('title', text: full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { 'Home' }
it_should_behave_like "all static pages"
end
#Other tests
spec_helper.rb看起来像(没有所有注释的东西)
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
我从rspec获得的错误都是这样的:
Static pages Home page it should behave like all static pages
Failure/Error: before { visit root_path }
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1::Nested_1:0x00000004a12210>
Shared Example Group: "all static pages" called from ./spec/controllers/static_pages_controller_spec.rb:17
# ./spec/controllers/static_pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
我已经尝试过使用
了 include Rails.application.routes.url_helpers
spec_helper中的,但它将我的错误更改为
Static pages Home page it should behave like all static pages
Failure/Error: Unable to find matching line from backtrace
SystemStackError:
stack level too deep
# /usr/lib/ruby/1.9.1/forwardable.rb:185
我也尝试过不同的方式重命名我的路线,但没有一种方法有效。我回到了教程版本。
如果它可以帮助我找到究竟是什么问题,我会使用rails 3.2.1和ruby 1.9.2p290来使用Ubuntu 11.10。希望你能提供帮助,我花了一段时间谷歌搜索解决方案,但没有找到任何^^'
答案 0 :(得分:107)
如果将以下内容放在rspec_helper.rb中,则命名路由应该有效:
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end
你是这样设置的吗?
答案 1 :(得分:3)
在我的案例中,Capybara命令visit
未知......
错误:
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa49a73c>
由于Capybara 2.0必须使用文件夹spec/features
,所以capybara命令不再适用于文件夹spec/requests
。
这帮助了我: http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html
希望你觉得这很有用。
答案 2 :(得分:2)
我不认为您可以访问rspec控制器规范中的命名路由。然而,您可以访问(&#39; /&#39;),这相当于root_path。
答案 3 :(得分:2)
我有同样的问题,使用相同的教程。事实证明,我需要重新启动Spork服务,一切正常。
Tom L发布的解决方案为我工作,但当我删除该行并重新启动Spork时,这也解决了问题。
希望能帮助那些对偏离教程代码感到谨慎的人!
答案 4 :(得分:1)
您应该使用
rails generate rspec:install
而不是
rspec --init
你不必修改配置文件。
现在不要这样做,或者你的申请会破裂,你将不得不浪费更多的时间来弄清楚它为何会破产。
答案 5 :(得分:0)
命名路线应该可以工作:
在我的rails_helper.rb代码处结帐
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
if Rails.env.production?
abort('The Rails environment is running in production mode!')
end
require 'rspec/rails'
require 'capybara/rails'
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include Warden::Test::Helpers
end
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
end