[rspec-users] Mocking ActiveResource

aslak hellesoy aslak.hellesoy at gmail.com
Wed Feb 7 06:49:40 EST 2007

On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote:
> On 2/7/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:
> > On 2/7/07, Pat Maddox <pergesu at gmail.com> wrote:
> > > I want to use ActiveResource in my app.  Instead of hitting server
> > > though, I want it to load from a file when I call find.  Any clue how
> > > I do that?
> > >
> >
> > In the same way as you'd mock ActiveRecord I would think. Is this not
> > working for you?
> No I want it to actually go through the process of finding something,
> but just have it use a file rather than an actual server.

I see, so you don't want to mock ActiveResource, but the transport used by it.

This is an approach that I generally don't recommend. The lower level
you're mocking at (HTTP, database connections, File I/O etc) the more
you have to set up and things become very verbose and fragile. As a
general rule:

  Don't mock APIs you don't own.

Why don't you just mock at a higher level - your ActiveResource classes?

The ActiveResource classes themselves I would verify against the real
transport, without mocking. The same goes for ActiveRecord. Their
specs run against the database, but specs for anything *using*
ActiveRecord/ActiveResource will talk to mock instances.


> Takes a couple steps (with my current solution):
> 1. Mock Net::HTTP.new to return some mock http object
> 2. mock http_object.get to return a mock http response
> 3. mock http_response.body to return the contents of your file
> I think http_object has to have :null_object => true if you want to
> keep things simple.
> I ended up tossing ARes altogether, so I'm not actually using it at
> this point.  But that's at least pretty close to what I did.  Ugly,
> but it worked.
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list