This project is archived and is in readonly mode.
Use #to_param in generated controller tests.
Reported by Pat Nakajima | February 8th, 2009 @ 04:46 PM | in 2.x
Currently, when you generate a new scaffold, the generated
controller tests are calling .id
on a fixture like
so:
def test_should_get_show
get :show, :id => articles(:one).id
end
Since to_param
is the canonical thing to pass, we
should generate tests that conform to this standard:
def test_should_get_show
get :show, :id => articles(:one).to_param
end
This will lead to fewer broken tests after something as trivial as a slug change.
Comments and changes to this ticket
-
John Barnette February 9th, 2009 @ 06:41 PM
Doesn't
to_param
get called automatically? What about:def test_should_get_show get :show, :id => articles(:one) end
Or have I been Doing It Wrong all this time? :)
-
Pat Nakajima February 9th, 2009 @ 06:53 PM
So in the fresh Rails app I just generated, yours seems to work unless
to_param
is overridden. I think it might be worthwhile to putto_param
in the scaffold anyway, since it'll teach people "the right way". -
John Barnette February 9th, 2009 @ 09:14 PM
Also, looks like the call to
to_param
makes the log dump of the request parameters in test a lot easier to read. I'm +1 on this change, not that it's needed at this point. -
Adam Milligan February 10th, 2009 @ 12:51 AM
+1
The whole "it's called an ID but it's not an ID" thing is confusing enough as it is. No need to make it worse by generating wrong code.
-
Pat Nakajima February 11th, 2009 @ 07:48 AM
I should mention that all tests continue to pass with this patch (though there didn't seem to be any coverage specifically related to this change).
-
Repository February 13th, 2009 @ 09:32 AM
- State changed from new to committed
(from [0c956443964a20cbcc122e2d8c429b0cbb121828]) Changed scaffold generated controller tests to use #to_param.
Before, the generated controller tests were calling #id, which can change over time, making for brittle tests.
Signed-off-by: Michael Koziarski michael@koziarski.com [#1913 state:committed] http://github.com/rails/rails/co...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 1913 Use #to_param in generated controller tests. Signed-off-by: Michael Koziarski michael@koziarski.com [#...