新手问题 在这问大家一个在测试的时候用 rspec 的问题

caleb328 · 2014年03月17日 · 最后由 glz1992 回复于 2014年03月18日 · 2983 次阅读

我运行 bundle exec rspec spec/的时候 terminal 给我了下面的 error 信息, 跪求大神们来分析一下是哪里出了问题

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:asoption, 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 withresources` 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:indecomposed_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:ineach' 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:inroot' 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:inblock in ' 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:ineval_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' 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:inblock 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:inload' 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:ineach' 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:inreload!' 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:incall' 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:inupdater' 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:inblock 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:inrun' 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:inblock 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:ineach_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:ineach' 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:intsort_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:ininitialize!' 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' 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' 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' 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:inblock 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:inload_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:inrun' 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'

先把 route.rb 里面的内容贴出来看看。

相同路由用了两次。

说句题外话,测试可以一直跑着,这样有什么错误就能及早发现。。。如果再加上个 git 什么的,那么错误可以很容易被排除(其实我今天刚被一个非常低级的语法错误坑了好久。。。)。

add_route': Invalid route name, already in use: 'root' (ArgumentError)

太明显了呀

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'

这是我的 route.rb 的内容,root 看起来只用了一次哎~

#4 楼 @liwei78 恩,那行我又看到,可是我看了一下 route.rb 里面 root 只用到了一次哎~

#1 楼 @glz1992 贴在六楼,大神给看看~

#8 楼 @caleb328 贴到 gist,好吧。。。算了我自己贴。。。

https://gist.github.com/liwei78/9618341

建议楼主先看看 resource 的方法,清理一下。

#9 楼 @liwei78 额=。=好吧。。。。大神有看出来什么问题么?

#11 楼 @caleb328 我可不是大神,我没看出啥问题,你再跑一下代码,把关键的部分贴到 gist 上

或者排除法,从一行 routes 开始,一个个加回来,看哪个的问题

#13 楼 @liwei78 是 root to 那行的问题。。。把那行注释掉就没事了。。。可是不知道那行为什么会有问题

等等。。。。你是 rspec 的时候出的这个问题,你要检查下 spec

#15 楼 @liwei78 有道理,我看看去

试试 rake routes 有没有问题。。。

#17 楼 @Kabie 我运行了 rake routes,只有一个 root,真的好奇怪啊,这个问题

#18 楼 @caleb328 我也是菜鸟啊,看你的 route 文件里没有错,你只是测试报错,而程序可以运行,还是都不能?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号