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

Ashley Moran work at ashleymoran.me.uk
Fri Feb 23 07:20:00 EST 2007

Three replies for the price of one...

I was about to reply to this:

On 22 Feb 2007, at 22:08, Nick Sieger wrote:
> Joe Walnes, "Don't mock types you don't own"

Idiot question... doesn't this preclude stubbing methods like  
ActiveRecord::Base.find?  Or have I misunderstood?

Then I saw this, which kinda answers it:

On 22 Feb 2007, at 22:32, David Chelimsky wrote:

> The problem is that in an effort to make life seemingly easier for you
> and me, AR forces us to couple our business logic with our persistence
> logic, which puts us in a catch 22 vis a vis pre-Rails thinking about
> testing and mocking. We can either test these things separately, which
> requires that we mock code we didn't write, or we adhere to only
> mocking code we don't own, which requires us to test two concepts
> together.
> Alternatively, we could add concepts to our rails apps to decouple
> these things - non-AR model classes that delegate to AR backed models
> - ModelFactories so we don't call class methods on the model classes,
> etc. My instinct is that it would be more complicated that its worth.
> Has anyone experimented w/ anything like that?

Not tried that, but I've been thinking for a while.  Is it time  
ActiveRecord became a data mapper, that defaulted to model -> table  
mapping?  I mean something like,

   class Person
     persistence_mapper :auto

     # usual stuff
     validates_presence_of :name

creates dynamically:

   PersonMapper < ActiveRecord::Base

and if you want more control,

   class FinanceProposal
     # make it call ComplexFinanceProposalMapper.new or whatever
     persistence_mapper :complex_finance_proposal_mapper

   class FinanceProposalMapper
     # all the overcomplicated stuff you need to do to save finance  
     # possibly using ActiveRecord::Base subclasses to save to tables

This way you get separation of validation so you can test it  
independently, without losing any of the out-of-the-box simplicity.

On 22 Feb 2007, at 21:33, Steven Baker wrote:
> The way you describe is the way to do this.  It sucks, but it's not
> our fault, it's ActiveRecord's fault.  AcitveRecord has extra
> suckage, baked in for free.  You don't even have to pay extra.

It seems a bit unfair to throw too much criticism at ActiveRecord.   
After all, it was intended to follow the ActiveRecord pattern for  
simplicity (presumably convention over configuration).  On the other  
hand, seeing as testability is pretty essential for agile  
development, which is one of the big selling points of Rails, it  
sounds like something has to be done.  Might make more sense to take  
this to the Rails list, but I seem to spend all my time here lately.

Has anyone got any experience of Og?  I looked at the source recently  
but haven't tried to use it.  How does it compare test-wise?


More information about the rspec-users mailing list