rails integration tests hardcoded routes problem
We had a problem with hard-coded routes in integration tests while using mixed RESTful and default routing. For example we can have in a integration test:
test "my sample test" do get 'foo_controler/bar_action' end
This looks not too nice, a string-hard-coded route to controller and its action.
The quick and fair solution we found was this one: First of all: do not use default routes, use RESTful routing only (see http://www.slideshare.net/ihower/rails-best-practices)
So we comment these things in routes.rb:
#map.connect ':controller/:action/:id' #map.connect ':controller/:action/:id.:format'
Second of all: use named routes instead of the default ones, so we add to routes.rb:
map.foo_controler 'foo_controler/:action/:id', :controller => :foo_controler
And change the integration test to:
test "my sample test" do get foo_controler_path(:action => :bar_action) end
Let me know what you think about that.
EDIT:
I am still learning much rails.
I came across another problem and found the “:only” parameter for the map.resources. This means that one should not use named routes. Only RESTfull routes should be used and the inclusion of the methods you want to implements is done by “:only” as the API says
This example is copied form the API reference:
map.resources :posts, :only => [:index, :show] do |post|
post.resources :comments, :except => [:update, :destroy]
end
Example of adding new, non-standard, routes:
map.resources :users, :collection => {
:show_profile => :get,
:edit_profile => :get,
:update_profile => :post
}
So the above integration test will look diferent. The helper methods can be found by listing the routes with “rake routes”.