对于我的测试,我有以下几点:
test "should update holder" do holder = Holder.create(name: "name",user_id: 10) put :update,holder: holder assert_redirected_to holder_path(assigns(:holder)) end
当我运行它,我得到以下错误:
ERROR (0:00:00.185) test_should_update_holder No route matches {:holder=>"980190963",:controller=>"holders",:action=>"update"} @ /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:532:in `raise_routing_error' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:528:in `rescue in generate' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:520:in `generate' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:561:in `generate' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:557:in `generate_extras' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_dispatch/routing/route_set.rb:553:in `extra_keys' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:147:in `assign_parameters' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:453:in `process' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:49:in `process' /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.2.3/lib/action_controller/test_case.rb:390:in `put' test/functional/holders_controller_test.rb:36:in `block in <class:HoldersControllerTest>' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:35:in `block in run' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:458:in `_run__4148286245602197272__setup__4285546581512185515__callbacks' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:405:in `__run_callback' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:81:in `run_callbacks' /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/testing/setup_and_teardown.rb:34:in `run'
但在我的路线我有:
Teacherjoy::Application.routes.draw do get "users/new" resources :questions resources :pages resources :holders resources :users resources :sessions,only: [:new,:create,:destroy] root :to => 'pages#home' match '/signup',to: 'users#new' match '/signin',to: 'sessions#new' match '/signout',to: 'sessions#destroy',via: :delete end
耙路返回:
[teacherjoy (master)]$rake routes users_new GET /users/new(.:format) users#new questions GET /questions(.:format) questions#index POST /questions(.:format) questions#create new_question GET /questions/new(.:format) questions#new edit_question GET /questions/:id/edit(.:format) questions#edit question GET /questions/:id(.:format) questions#show PUT /questions/:id(.:format) questions#update DELETE /questions/:id(.:format) questions#destroy pages GET /pages(.:format) pages#index POST /pages(.:format) pages#create new_page GET /pages/new(.:format) pages#new edit_page GET /pages/:id/edit(.:format) pages#edit page GET /pages/:id(.:format) pages#show PUT /pages/:id(.:format) pages#update DELETE /pages/:id(.:format) pages#destroy holders GET /holders(.:format) holders#index POST /holders(.:format) holders#create new_holder GET /holders/new(.:format) holders#new edit_holder GET /holders/:id/edit(.:format) holders#edit holder GET /holders/:id(.:format) holders#show PUT /holders/:id(.:format) holders#update DELETE /holders/:id(.:format) holders#destroy 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 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 root / pages#home signup /signup(.:format) users#new signin /signin(.:format) sessions#new signout DELETE /signout(.:format) sessions#destroy
如果你看耙子路线,显然是一个更新的动作,这是一个放置,在持有者控制器,这是我的测试正在做的,对吧?
解决方法
请注意,该路由实际上是PUT /holders /:id,但是您将:持有者选项传递给put方法,而不是:id.尝试将测试中的那一行更改为:
put :update,id: holder