Rails units testing: No route matches why? -


i'm trying units test, first time.

i don't understand why there no route matches.

when run $ rake test test/controllers/products_controller_test.rb

i in output of console:

1) error: productscontrollertest#test_should_get_edit: actioncontroller::urlgenerationerror: no route matches {:action=>"edit", :controller=>"products"}     test/controllers/products_controller_test.rb:20:in `block in <class:productscontrollertest>'     2) error: productscontrollertest#test_should_get_show: actioncontroller::urlgenerationerror: no route matches {:action=>"show", :controller=>"products"}     test/controllers/products_controller_test.rb:35:in `block in <class:productscontrollertest>'     3) error: productscontrollertest#test_should_get_create: actioncontroller::parametermissing: param missing or value empty: product     app/controllers/products_controller.rb:59:in 'product_params'     app/controllers/products_controller.rb:18:in 'create'     test/controllers/products_controller_test.rb:15:in `block in <class:productscontrollertest>'     4) error: productscontrollertest#test_should_get_update: actioncontroller::urlgenerationerror: no route matches {:action=>"update", :controller=>"products"}     test/controllers/products_controller_test.rb:25:in `block in <class:productscontrollertest>'     5) error: productscontrollertest#test_should_get_destroy: actioncontroller::urlgenerationerror: no route matches {:action=>"destroy", :controller=>"products"}     test/controllers/products_controller_test.rb:30:in `block in <class:productscontrollertest>'  7 runs, 2 assertions, 0 failures, 5 errors, 0 skips 

this products_controller_test.rb file:

require 'test_helper'  class productscontrollertest < actioncontroller::testcase   test "should index"     :index     assert_response :success   end    test "should new"     :new     assert_response :success   end    test "should create"     :create     assert_response :success   end    test "should edit"     :edit     assert_response :success   end    test "should update"     :update     assert_response :success   end    test "should destroy"     :destroy     assert_response :success   end    test "should show"     :show     assert_response :success   end  end 

routes.rb file:

rails.application.routes.draw   resources :products end 

for these routes (edit, update, destroy), need which product you're editing/updating/destroying. if rails doesn't know it, can't draw route you.

for edit, example, full route products/:product_id/edit. rails need 'fill in' :product_id key. if leave blank route breaks.

in code, then, if you're calling get :edit, need specify product id. so:

get :edit, product_id: products(:test_product).id

(using fixtures explained in rails test tutorial here)


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -