On 1 December 2010 17:22, Andrew M. Kasper <[email protected]> wrote: >> I have a named scope that sets a sort order. In my unit test I can >> run the query on a set of records (using Factory or fixtures) and >> check the sort order is correct. I believe though that a find without >> an explicit sort order may return the records in any order, so how can >> I be sure that it is not just accidental that they are in the correct >> order? > > I would argue that it's unimportant -- even at the unit-level -- *how* > the test is passing. If every time you run the test it passes, then > the code does what you specify it does. When there's a problem, > identify the bug and write a new unit, integration, or functional test > that eliminates the unexpected behavior, then make that test pass.
I don't think that is a valid argument. I think effectively what you are saying is that it doesn't matter if it actually tests what it is supposed to as long as it passes. If you later discover that the app is not doing what it is supposed to then add more tests. > > I might also argue that it's not up to you to test ActiveRecord. You > might assert that your named scope is passing the correct sorting > parameters to ActiveRecord; at that point, it's up to ActiveRecord to > hold up its end of the bargain. I am not trying to test that ActiveRecord sorts correctly, I am trying to test that I have correctly coded the scope. I don't know how I would assert that the named scope is passing the correct sorting params. Can you enlighten me? I think what I will do is use some common sense rather than being too pedantic about the testing. By creating a number of records in a random order and then checking that they are sorted correctly I think the possibility of accidentally finding them come back in sorted order is negligible. In addition I will have checked that the test fails before I add the sort clause. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

