[mocha-developer] Automatically mocking ActiveRecord methods

James Moore jamesthepiper at gmail.com
Mon Apr 2 12:24:02 EDT 2007


After I wrote this, I started to have second thoughts.  It seemed like this
was mostly useful in Rails controller tests as a way of abstracting away the
database.  But then I started running into too many situations where I had
handcrafted SQL in use, and mocking that clearly was the wrong way to go.
(We're storing something that looks like a directory tree in SQL records,
and the live installation has a few million entries.  The standard rails
things like acts_as_tree are hopeless for performance without handcrafting
some queries.)

I'm now wondering whether a better way to do this is to add a facility to
add callbacks that can create mocks and stubs when a particular object is
instantiated from the database.  A sort of cleanup method on Rails find*()
methods that will allow you to do mock/stub methods on an object with a
particular id (or some set of criteria) when it's created.

In any case, I agree that it should be some sort of addon facility, and I
don't think there's any reason why it would need to touch Object directly.

 - James Moore

On 3/27/07, James Mead <jamesmead44 at gmail.com> wrote:
>
> I can see why you might want to do this. I'm not sure I would want to add
> another public instance method to Object - recently I've been looking into
> how to reduce the number of such methods added to Object. Also I don't
> really want to incorporate any more Rails-specific code into Mocha. The
> Mocha Rails plugin is currently little more than a conditional require
> statement.
>
> Can I suggest you consider creating your own Rails plugin and see how
> things
> go...?
>
> --
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer
>


More information about the mocha-developer mailing list