[rspec-users] Testing two interdependent ActiveRecord models
mailing_lists at railsnewbie.com
Thu Jun 21 16:00:11 EDT 2007
On Jun 21, 2007, at 3:17 PM, David James wrote:
> Dear geeky, pay-your-debt-up-front-instead-of-later testing guru
> (tell me I'm wrong!)...
> I would appreciate if you (twould take a look at my message from a
> few weeks ago, since I didn't get any replies. Any comments would
> appreciated -- even if they are "you are barking up the wrong tree"
> -- or "you should read article ____ so that you can rephrase /
> rethink your question"
> On Jun 8, 2007, at 9:05 AM, David James wrote:
>> 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. :)
As far as I can tell, BDD seems to be higher level than typical
testing ("Unit testing" - What would the "Unit" be here?). So it
would make sense (and you are certainly able) to test both models in
one "describe" block. If you want more granular "unit" testing,
write a person spec and a contributions spec separately (and maybe in
addition to this). You may want to watch the Dave Astels video on
Google Videos relating to this topic:
With that said, mocking/stubbing will probably be helpful in this
shared spec. Or you could just mock/stub the opposite model of the
one you are testing (i.e. when testing Person, mock Contributors and
For me, though, it's hard to see why you couldn't use the later
method (of mocking the association). Maybe you could give us
something a little more concrete and/or post some code?
Hope that helps,
>> I would also be open to a refactoring / redesign if that will help
>> with testing.
>> rspec-users mailing list
>> rspec-users at rubyforge.org
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users