[rspec-users] How far to go with ActiveRecord unit tests without hitting the database?
davidj503 at gmail.com
Tue Sep 18 09:23:31 EDT 2007
> "But otherwise, what exactly are you testing?"
That's a great point. I don't want to be specing Rails if it is not easily
separable for testing already. In theory, I would like to have true unit
tests for my models. However, in practice, some of my models are coupled to
Generally speaking, I prefer not to mock/stub Rails internals to test my
app. I don't find mocking/stubbing *my* code -- that is fine with me. But
the internals of Rails are *not* what I'm trying to test.
In particular, I found that mocking/stubbing with has_many :through was far
from simple. I fired up ruby-debug (highly recommended!) and everything,
but realized it was too much effort and too little time. In this sense, my
experience is probably similar to Scott's (above post).
My Goal model, for example, checks to see how many Contributions it has. It
uses a has_many :through (hmt) association. Despite the helpful words of
some posters in this thread, I couldn't find a way to effectively spec the
hmt association. By the time I had stubbed out enough to make it a true
unit test, I was no longer testing what I wanted to test -- namely, that the
Maybe this was a wake up call that there was not point in trying to test
that at a unit level? In any case, I've got some conditional logic that
connects with the association, so I definitely need to test that, at least.
In the end, I just tested the "interaction" between models. In this folder,
I test how models in my app work together. I didn't think these specs fit
under 'model' because they are testing more than one model.
So I put them in a new directory called 'model_integration' folder under the
'spec' directory. I came up with this name on my own intuition, and I can't
blame anyone else if this is a bad idea. I wasn't sure if
'model_interaction' would have been a better name, but from what I gather,
'interaction' is a special term when it comes to testing. Suggestions?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the rspec-users