[rspec-users] Testing two interdependent ActiveRecord models

David James dj at collectiveinsight.net
Fri Jun 8 10:05:32 EDT 2007


Hello,

In my app, I'd like to test two model object objects that interact  
with each other.  (In my example, a Person can have Contributions.)   
I guess you could say that I'm testing ActiveRecord associations.   
For example, a Person has zero or many Contributions.  A Contribution  
has one or more People.  This is the very basic version, but I think  
it will illustrate the dependency.  (In practice, I'm interested in  
testing these associations because there is some extra logic going on  
that needs testing.)

Here are my observations / questions:
1. Since there are two models, it doesn't seem quite right to make a  
unit test for this, does it?
2. There is an interaction between the two models.  What is a good  
way to test the interaction?  Do I put some of the testing in  
person_spec.rb and some in contribution_spec.rb?  Or do I create some  
sort of "interaction" spec?
3. I don't think that an integration test makes sense, because I  
don't want to test the controller and views.
4. I wonder if mocks or stubs would make sense.  I don't think I need  
to stub out a particular method (i.e. credit card processing),  
though.  I'm pretty new to mocking, any advice or perspective would  
be helpful.

I would appreciate any feedback -- testing philosophy, and maybe some  
hand waving, if you like. :)

I would also be open to a refactoring / redesign if that will help  
with testing.

Thanks,
-David




More information about the rspec-users mailing list