当我使用rspec:无效的路由名称,已经在使用:'root'(ArgumentError)

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

标签: ruby-on-rails rspec

当我使用rspec进行一些测试时:bundle exec rspec spec/终端会向我抛出此错误。但在我的route.rb中,我认为我只定义了一个根路径

CalebdeMacBook-Pro:sample_app Caleb$ bundle exec rspec spec/
/Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/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 /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1445:in `add_route'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1422:in `decomposed_match'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1403:in `block in match'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1394:in `each'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1394:in `match'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:330:in `root'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/mapper.rb:1464:in `root'
    from /Users/Caleb/rubyonrails/sample_app/config/routes.rb:13:in `block in <top (required)>'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:341:in `instance_exec'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:341:in `eval_block'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/actionpack-4.0.3/lib/action_dispatch/routing/route_set.rb:319:in `draw'
    from /Users/Caleb/rubyonrails/sample_app/config/routes.rb:1:in `<top (required)>'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `block in load'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:223:in `load'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:in `each'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/file_update_checker.rb:75:in `call'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.3/lib/active_support/file_update_checker.rb:75:in `execute'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/routes_reloader.rb:6:in `execute_if_updated'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/initializable.rb:30:in `run'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
    from /Users/Caleb/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/application.rb:215:in `initialize!'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /Users/Caleb/rubyonrails/sample_app/config/environment.rb:5:in `<top (required)>'
    from /Users/Caleb/rubyonrails/sample_app/spec/spec_helper.rb:3:in `require'
    from /Users/Caleb/rubyonrails/sample_app/spec/spec_helper.rb:3:in `<top (required)>'
    from /Users/Caleb/rubyonrails/sample_app/spec/models/event_spec.rb:1:in `require'
    from /Users/Caleb/rubyonrails/sample_app/spec/models/event_spec.rb:1:in `<top (required)>'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
    from /Users/Caleb/.rvm/gems/ruby-2.0.0-p353/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

routes.rb中:

SampleApp::Application.routes.draw do  
  resources :users  
  resources :sessions, only: [:new, :create, :destroy]  
  resources :events do  
    member do  
      get :members  
    end  
    collection do  
      get :search  
    end  
  end  
  resources :eventjoinings, only: [:create, :destroy]  
  root to: 'static_pages#home'  
  match '/signup',  to: 'users#new',            via: 'get'  
  match '/signin',  to: 'sessions#new',         via: 'get'  
  match '/signout', to: 'sessions#destroy',     via: 'delete'      
  match '/help',    to: 'static_pages#help',    via: 'get'  
  match '/about',   to: 'static_pages#about',   via: 'get'  
  match '/contact', to: 'static_pages#contact', via: 'get'  
  match '/create',  to: 'events#new',           via: 'get'  
end

rake routes:

        users GET    /users(.:format)              users#index  
              POST   /users(.:format)              users#create  
     new_user GET    /users/new(.:format)          users#new  
    edit_user GET    /users/:id/edit(.:format)     users#edit  
         user GET    /users/:id(.:format)          users#show  
              PATCH  /users/:id(.:format)          users#update  
              PUT    /users/:id(.:format)          users#update  
              DELETE /users/:id(.:format)          users#destroy  
     sessions POST   /sessions(.:format)           sessions#create  
  new_session GET    /sessions/new(.:format)       sessions#new  
      session DELETE /sessions/:id(.:format)       sessions#destroy  
members_event GET    /events/:id/members(.:format) events#members  
search_events GET    /events/search(.:format)      events#search  
       events GET    /events(.:format)             events#index  
              POST   /events(.:format)             events#create  
    new_event GET    /events/new(.:format)         events#new  
   edit_event GET    /events/:id/edit(.:format)    events#edit  
        event GET    /events/:id(.:format)         events#show  
              PATCH  /events/:id(.:format)         events#update  
              PUT    /events/:id(.:format)         events#update  
              DELETE /events/:id(.:format)         events#destroy  
eventjoinings POST   /eventjoinings(.:format)      eventjoinings#create  
 eventjoining DELETE /eventjoinings/:id(.:format)  eventjoinings#destroy  
       signup GET    /signup(.:format)             users#new  
       signin GET    /signin(.:format)             sessions#new  
      signout DELETE /signout(.:format)            sessions#destroy  
         help GET    /help(.:format)               static_pages#help  
        about GET    /about(.:format)              static_pages#about  
      contact GET    /contact(.:format)            static_pages#contact  
       create GET    /create(.:format)             events#new  
         root GET    /                             static_pages#home  

1 个答案:

答案 0 :(得分:2)

这两条命名路线:

match '/signin',  to: 'sessions#new',         via: 'get'  
match '/signout', to: 'sessions#destroy',     via: 'delete'

覆盖使用以下设置的路线:

resources :sessions, only: [:new, :create, :destroy] 

将该行更改为:

resources :sessions, only: [:create] 

假设你甚至需要那条路线。