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 (
id SERIAL PRIMARY KEY
, foo_name TEXT NOT NULL UNIQUE
class FooTest < Test::Unit::TestCase
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
@found_first_foo_directly = Foo.find(foos(:first_foo).id) #
assert_equal @first_foo.id, @found_foo.id # actually test
assert_equal @first_foo.id, @found_first_foo.id # 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.
grzm myrealbox com