Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tests say no routes match, but they work in browser

For my test I have the following:

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

And when I run them I get the following Error:

  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'

But in my routes I have:

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

and rake routes returns:

[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

If you look at rake routes, there clearly is a an action for update, which is a put, in the holders controller, which is what my test is doing, right?

like image 804
Noah Clark Avatar asked Jun 19 '12 21:06

Noah Clark


1 Answers

Notice that the route is actually PUT /holders/:id, but you're passing a :holder option to your put method, not an :id. Try changing that line in your test to this:

put :update, id: holder
like image 197
Brandan Avatar answered Nov 08 '22 00:11

Brandan