Rspec加载routes.rb两次

时间:2014-03-27 18:35:07

标签: rspec routes capybara

我有一个问题,Rspec加载路由两次并因此抛出错误。 我一直试图弄清楚这几天没有运气。搜索互联网并不会带来太大的影响。当我运行rspec时,它会发出以下错误:

/home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:434:in `add_route': Invalid route name, already in use: 'root'  (ArgumentError)
You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: 
http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1445:in `add_route'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1422:in `decomposed_match'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1403:in `block in match'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1394:in `each'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1394:in `match'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:330:in `root'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/mapper.rb:1464:in `root'
from /home/user/Documents/app/config/routes.rb:21:in `block in <top (required)>'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:341:in `instance_exec'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:341:in `eval_block'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.1/lib/action_dispatch/routing/route_set.rb:319:in `draw'
from /home/user/Documents/app/config/routes.rb:1:in `<top (required)>'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:223:in `load'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:223:in `block in load'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
from /home/v/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:223:in `load'
from /home/v/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:40:in `each'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/file_update_checker.rb:75:in `call'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.1/lib/active_support/file_update_checker.rb:75:in `execute'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:27:in `updater'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/initializable.rb:30:in `instance_exec'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
from /home/user/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/initializable.rb:54:in `run_initializers'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/application.rb:215:in `initialize!'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/user/Documents/app/config/environment.rb:5:in `<top (required)>'
from /home/user/Documents/app/spec/spec_helper.rb:3:in `require'
from /home/user/Documents/app/spec/spec_helper.rb:3:in `<top (required)>'
from /home/user/Documents/app/spec/features/registration_pages_spec.rb:1:in `require'
from /home/user/Documents/app/spec/features/registration_pages_spec.rb:1:in `<top (required)>'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /home/user/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'

这是我的routes.rb文件:

App::Application.routes.draw do
  root 'registrations#new'
end

还有其他内容,但我可以将它们全部评论出来并仍然得到错误。因此它们无关紧要。

spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
#Capybara
require 'capybara/rspec'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  config.include Helpers

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

我的Gemfile的一部分

group :development, :test do
  gem 'launchy'
  gem 'rspec-rails', '~> 2.0'
  gem 'capybara', '~> 2.1.0'
  gem 'factory_girl_rails', '~> 4.0'
end

问题是Rspec正试图加载routes.rb两次。我通过将puts "here"放在routes.rb文件中来测试它。为了爱SO,我无法弄清楚为什么,或者如何调试这个。 当我运行rake routes时,它只显示一个root路由,如果我在routes.rb中注释掉它,那么它就不会显示。似乎一切都是正确的,除了rspec不起作用。 此外,这是一个类似的问题When I use rspec: Invalid route name, already in use: 'root' (ArgumentError),但它不一样(OP不清楚) - 我知道在我的情况下,原因是因为Rspec正在加载两次路由。请帮助,我真的坚持这个。现在任何建议都会很好。

0 个答案:

没有答案