[rspec-users] do I use mocking (vs fixtures) for methods that produce results based on non-trivial SQL queries

Scott Taylor mailing_lists at railsnewbie.com
Fri Sep 5 10:37:56 EDT 2008


On Sep 5, 2008, at 10:08 AM, Greg Hauptmann wrote:

> Hi,
>
> In cases where I have model methods that are generating data based on
> non-trivial SQL queries to a database (e.g. to pull together data
> required to plot an appropriate graph), does this really require a
> traditional fixture (i.e. pre-canned test data) as opposed to use of
> mocks?
...
> it should.   How would one use rspec / mocha to solve this?  i.e. in a

You tell me.  Actually, there is no way, because stubs operate at a  
higher level on the call stack.  Until rails treats *SQL* as an object  
(i.e., until there is a ruby SQL parser out there and rails uses it),  
stubbing will never be able to test real database activity.  This is  
why rspec still recommends using real database rows in your tests -  
not because it's an apriori requirement for testing against SQL data,  
but because it is currently the only way.

Of course, the reason why people don't like this answer is not really  
for the reason people like mocks otherwise (functional isolation).   
Instead, its because a large test suite hitting the database will  
become very slow over time.  Just do the math: If one database test  
takes 0.2 sec (a realistic figure), then 5 will take 1 sec, at 1000  
over 3 minutes, and at 2000 over 6 minutes.  This is starting to get  
into "time for a coffee/cigarette/juggling what have you" breaks.  So  
- either you end up very hyped up on coffee not doing much work, or  
you don't run your test suite very often, which leads to legitimate  
bugs which can be solved. (At one point I had actually deployed code  
into production which had a serious bug.  This all could have been  
avoided because the test suite *was failing*, but I had gotten into  
the habit of not running it because it was too expensive).

I've been working hard on solving this issue by building a SQL parser  
in treetop, and hope anyone who is interested will join me (just let  
me know if you are interested and I'll make the project publicly  
viewable on github).

Scott


More information about the rspec-users mailing list