Find, instantiated fixtures, and testing

#1

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

db/ddl.sql:
CREATE TABLE foo (
id SERIAL PRIMARY KEY
, foo_name TEXT NOT NULL UNIQUE
);

test/fixtures/foos.yml:
first_foo:
id: 1
foo_name: ur-foo

test/unit/foo_test.rb:
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(@first_foo.id) # Method 2: from

instantiated fixture
@found_first_foo_directly = Foo.find(foos(:first_foo).id) #
Method 3

     assert_equal @first_foo.id, @found_foo.id # actually test

something
assert_equal @first_foo.id, @found_first_foo.id # testing
with Method 2 test object
end
end

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
p150)
AWDwR errata
[Mike C.'s explanation](http://www.clarkware.com/cgi/blosxom/
2005/10/24#Rails10FastTesting
)