[rspec-users] We can't 100% remove our unit tests from the database, can we?

Nick Sieger nicksieger at gmail.com
Thu Feb 22 17:08:29 EST 2007


On 2/22/07, Pat Maddox <pergesu at gmail.com> wrote:
>
>
> specify "should find all users over the given age" do
>   User.should_receive(:find).with(:all, :conditions => ["age > ?", 18])
>   User.find_older_than 18
> end


I don't have a good suggestion here, but an observation is that the spec is
too fine-grained and brittle, which, as you mention below, inhibits
refactoring and TDD flow, etc.

I don't know about you, but to me that sucks.  There is no TDD rhythm
> there.  We write a failing spec, but it's a fairly complex spec.  On
> top of that we've basically implemented the method from within the
> spec.  That's not TDD.
>
> We also can't refactor.  Let's say that at some point we decided to
> change the method to
>
> def User.find_older_than(age)
>   find(:all).select {|u| u.age > age }
> end
>
> Our spec would break, even though the semantics of
> User.find_older_than hasn't changed.


I have experienced similar troubles as you have in the past, and the usage
of mock-based testing in these scenarios still hasn't fully clicked for me
either.  I know something's not right, but I can't put my finger on what it
is.  I suspect it's that the APIs you're working with are too data-intensive
and not message-intensive (i.e., "tell don't ask"), which is where mock
testing really shines.  I'll defer to this post from Steve Freeman (there
are probably other entries too that are similar), and the quote that
apparently originates from Joe Walnes, "Don't mock types you don't own".

http://stevef.truemesh.com/archives/000194.html

I'm beginning to think that most of the time, you don't want to use a
> test DB at all.  When you're testing an AR model, just use an
> in-memory record and stub any associations you need.  But when you do
> do something that interacts with the database - like doing a custom
> find - you need to use the database to make sure that you're getting
> the right results.  Whether you use fixtures or create some records in
> the setup method, you have to actually hit the db.


I don't have a better answer, so maybe your instinct is right here -- don't
fight with brittle mocks, if the database works for you and doesn't slow you
down too much.

I would think Dave, David and Aslak would have good input on this topic, and
hope they have a moment to share their experiences.

Cheers,
/Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070222/0dfbdffa/attachment-0001.html 


More information about the rspec-users mailing list