Unit testing and stale ActiveRecords

Not sure what Joshua’s problem is, drunk maybe?

Anyway, why aren’t you using Fixtures? This is exactly what they’re for.

Jason

Could you paste an example of a problematic unit test and
corresponding production code?

Pat

Thanks for the reply Frederick,

How am I supposed to use fixtures to test the creation of dynamic
objects? I can’t figure out a reliable method of save/load that works.
For example, lets say I have a user model. This user has a method for

joining a club which is represented by a memberships ‘through’ table. I
want to test that calling user.join_club© creates a membership record
and binds it to the correct user and club. However, when I construct

that new object and save it to the database both user.memberships[0] and
club.memberships[0] are not populated; let alone user.clubs[0] or
club.users[0] (the through relationships) No matter how many ways I

save and load the active records nothing seems to work as expected.

Would like to see your code. This sounds like you don’t have relationships set up correctly.

The other problem I have with yml fixtures is that it’s hard to build
relationships with them since they only represent one table. I guess I
could manually connect them with the ‘id’ field across multiple yml

files, but that’s pretty clunky compared to creating only what you need
in the specific test. It also means lots of objects in my fixtures
which clutters things up and makes it unclear what exactly is required

to demonstrate the functionality being tested.

It sounds like you don’t like thinking about the database. Unless you have an absolute ton of relationships (questionable in itself, of course), it’s not hard to put together data exactly as you want for testing. You do need to understand what goes on in the database itself and what ActiveRecord expects to properly use Rails with AR.

You are of course able to write ActiveRecord code to build objects (such as in setup, or in a separate file required in tests), but that requires that your relationships are set up correctly. If you’re using ActiveRecord, you need to manually set up data in fixtures to test your relationships.

Is there something you’re confused about with fixtures or ActiveRecord testing?

I’ve been searching for information about best-practices in unit testing
rails and this information just doesn’t seem to exist. Most of the
examples are pretty brain-dead and don’t deal with relationships and

object interactions which is where most of the bugs and interesting
design problems are.


Sean

class MyModelTest < Test::Unit::TestCase
fixtures :my_models, :pieces

def test_has_many_pieces
model = MyModel.new
assert model.respond_to?(:pieces)

assert_equal 3, model.pieces.size

end
end

Please define “brain-dead”. It unit-testing supposed to be witty and exciting and something hard to figure out? Or do you need a tutorial on unit-testing itself?

Jason