Forum: Ruby on Rails find, instantiated fixtures, and testing

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
grzm (Guest)
on 2005-11-29 07:06
(Received via mailing list)
I'm trying to abstract out as much as possible in my tests (see
[Keeping Tests Flexible]). Testing has changed a bit since the third
printing of "Agile Web D. With Rails Errata" (see point
#1344 in [AWDwR errata] and [Mike C.'s explanation] of the
changes). I've found both AWDwR very useful. I have a question about
style that I'd like comments on.

Here's an example

     CREATE TABLE foo (
       , foo_name TEXT NOT NULL UNIQUE

       id: 1
       foo_name: ur-foo

     class FooTest < Test::Unit::TestCase
       fixtures :foos

       def test_create_first_foo
         first_foo_hash = @foos["first_foo"] # not necessary, just
included for completeness
         @first_foo = foos(:first_foo) # instantiate fixture
         @found_foo = Foo.find(1) # Method 1: instantiate object to test

         # alternatives to instantiating object to test
         @found_first_foo = Foo.find( # Method 2: from
instantiated fixture
         @found_first_foo_directly = Foo.find(foos(:first_foo).id) #
Method 3

         assert_equal, # actually test
         assert_equal, # testing
with Method 2 test object

I lean strongly towards Method 2, as this keeps repetition to a
minimum and guarantees that the fixture and test object are
instantiated from the same data.

I can't see any problem with this method, but being new to Ruby,
Rails, and testing, I very well may be missing something. Also, I
haven't seen examples where this method is used, which makes me
suspect there's a reason for *not* instantiating the test object this
way. Any ideas for a better way to doing this?

Thanks for any and all comments and feedback.

Michael G.
grzm myrealbox com

[Keeping Tests Flexible]: Agile Web D. With Rails, p141 (pdf
[AWDwR errata](
[Mike C.'s explanation](
This topic is locked and can not be replied to.