Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Use #to_param in generated controller tests.

#1913

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.

Reported by Pat Nakajima · February 8th, 2009 @ 04:46 PM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. David Stevenson
  2. Chad Woolley
  3. Matthew O'Connor
  4. Zach Brock
  5. Brandon Keene
  6. John Barnette
    John Barnette

    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? :)

    February 9th, 2009 @ 06:41 PM

  7. Pat Nakajima
    Pat Nakajima

    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 put to_param in the scaffold anyway, since it'll teach people "the right way".

    February 9th, 2009 @ 06:53 PM

  8. John Barnette
    John Barnette

    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.

    February 9th, 2009 @ 09:14 PM

  9. Adam Milligan
    Adam Milligan

    +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.

    February 10th, 2009 @ 12:51 AM

  10. David Yeu
  11. Jeff Dean
  12. Pat Nakajima
    Pat Nakajima

    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).

    February 11th, 2009 @ 07:48 AM

  13. Repository
    Repository
    • 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 Use #to_param in generated controller tests. state:committed] http://github.com/rails/rails/co...

    February 13th, 2009 @ 09:32 AM