我对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
我也尝试了不同的方式来重命名我的路线,但它们都没有奏效。我回到教程版。
如果它可以帮助找出究竟是什么问题,我在 Ubuntu 11.10 上,使用 rails 3.2.1 和 ruby 1.9.2p290。希望你能帮忙,我花了很长时间在谷歌上搜索解决方案,但没有找到任何解决方案^^'