[rspec-users] Testing Rails Associations

Daniel N has.sox at gmail.com
Wed Jul 4 00:38:25 EDT 2007


I'm very new to rspec, so if this is not the right forum please let me know.

I'm starting to spec my models first in an existing rails app, porting from
a mix of Test::Unit, and simply_bdd amongst others.

I'm at the point where I want to test that certain associations are
present.  What I'm not sure of is should I test the association or the
method and return object.

That is, if I wanted to test a has_many should I:

Confirm the methods exist, and that the return an array etc


Check that the model has the named has_many association through it's

On one hand the second one looks like it will be a bit more robust, since if
there is a has_many relationship, then all the associated methods may be
used througout the app.  This would put testing in the one place.

On the other hand, this would be really testing the implementation of the
model rather than it's behaviour.   The behaviour is to call @
article.comments and have an array of comments returned.

Any thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070704/8f209d4f/attachment.html 

More information about the rspec-users mailing list