On Dec 17, 2007, at 12:37 AM, ryan_s wrote:
If I approached this using test driven methodology I would write a
the better route from the example with less effort:
problem route and noticing the problem.
Testing something like whether or not a page redirects somewhere is
recommended, but going as far as to test something that’s core
Rails stuff,
I wouldn’t bother testing it. It should always do what you think it
should
do, unless you’re not using a stable release.
ryan_s,
I tend to put these tests in the same file that tests the controller
to which the route is connected. For the example in this message,
that’d be in test/functional/blog_controller_test.rb. But know that
there are two forms that your testing needs to take.
For a GET route, it’s as simple as (I always put a test that the
controller name is right, too):
def test__controller_naming
assert_equal(‘similarity’, @controller.controller_name,
“None of the routing tests will work if
controller_name is not ‘similarity’”)
end
for a non-standard default action
def test__route_similarity_categories
assert_routing ‘/similarity’, { :controller =>
‘similarity’, :action => ‘categories’ }
end
for a nested-ish resource
def test__route_similarity_candidates
assert_routing(‘/similarity/1/group/2’,
{ :controller => ‘similarity’, :action => ‘list’,
:id => ‘1’, :trait_group => ‘2’ })
end
However, if you want to test that the specific kind of request is
used, you need to use the two separate methods that assert_routing
uses internally because there’s no way to pass all the needed options
through.
Need to split these out in order to require that POST is used.
def test__route_similarity_make_similar
assert_recognizes({ :controller => ‘similarity’, :action =>
‘make_similar’,
:id => ‘1’, :trait_group => ‘2’, :leader =>
‘3’, :follower => ‘4’ },
{ :path => ‘/similarity/1/group/2/3/4’, :method
=> :post })
assert_generates('/similarity/1/group/2/3/4',
{ :controller => 'similarity', :action =>
‘make_similar’,
:id => ‘1’, :trait_group => ‘2’, :leader =>
‘3’, :follower => ‘4’ })
end
This route happens to be for an AJAX’d action so less fluff in the URL
wasn’t a concern.
-Rob
Rob B. http://agileconsultingllc.com
[email protected]