Best way to stub out calls during tests

279 views
Skip to first unread message

cyx

unread,
Jul 22, 2009, 10:31:08 AM7/22/09
to Sunspot
Hi,

What's the ideal way to stub out the requests during tests, and then
in some cases, switch it back on?

Thanks in advance.

Matthew A. Brown

unread,
Jul 22, 2009, 10:50:20 AM7/22/09
to ruby-s...@googlegroups.com
Hi,

The simplest thing is probably to use a mocking framework like Mocha
or RSpec's built-in mocking to stub out the calls to Sunspot's index -
something like this

class SomeTest < ActiveSupport::TestCase
def setup
[:index, :commit].each { |method| Sunspot.stub(:method) }
end
end

The stub will only last for the duration of the test, so you'd just
want to not include that call for tests that interact with Solr.

One thing I'd like to do in the future would be to give an option to
"turn off" Sunspot, basically making all of the Solr calls noops - but
the stubbing solution should work fine for now.

Hope that helps!

Mat

cyx

unread,
Jul 22, 2009, 12:04:31 PM7/22/09
to Sunspot
Ok thanks!

Yep, hmm i'll probably alias the 2 methods you mentioned in the
meantime since most of my tests will hit solr and it'd be a slight
pain to add stub calls to the majority of my tests.

But yeah, thanks at least I know which methods to disable now!

Simone Carletti

unread,
Nov 9, 2009, 8:53:31 AM11/9/09
to cyx, ruby-s...@googlegroups.com
Hi Mat & cyx,
I recently experienced the same problem just want to share with you my
solution.

First I went with Mat's solution. I'm already using Mocha so it was
easy. The original code has some typo, here's the right version.

def setup
[:index, :commit].each { |method| Sunspot.stubs(method) }
end

Unfortunately this is a partial solution. That's because my code also
calls :remove and I didn't want to end up stubbing each
Sunspot.methods all the time.

So I decided to follow a different approach. Sunspot module is
extremely well coded and all methods rely on the session instance.
Just mock the session object and you're done.

Here's the result http://gist.github.com/229940

The code is quite tied to a Rails app. Having it coded in the main
Sunspot library won't require all the metaprogramming used to
intercept standard Sunspot calls.
I can create a patch, if you find it useful.

Regards,
-- Simone


On Jul 22, 5:04 pm, cyx <cyx.uc...@gmail.com> wrote:
> Ok thanks!
>
> Yep, hmm i'll probably alias the 2 methods you mentioned in the
> meantime since most of my tests will hit solr and it'd be a slight
> pain to add stub calls to the majority of my tests.
>
> But yeah, thanks at least I know which methods to disable now!
>
> On Jul 22, 10:50 pm, "Matthew A. Brown" <mat.a.br...@gmail.com> wrote:
>
> > Hi,
>
> > The simplest thing is probably to use a mocking framework like Mocha
> > or RSpec's built-in mocking to stub out the calls to Sunspot's index -
> > something like this
>
> > class SomeTest < ActiveSupport::TestCase
> >   def setup
> >     [:index, :commit].each { |method| Sunspot.stub(:method) }
> >   end
> > end
>

> > The stub will only last for the duration of thetest, so you'd just

Reply all
Reply to author
Forward
0 new messages