[mocha-developer] Fwd: Mocha raise exception first call, return value second call

James Mead jamesmead44 at gmail.com
Fri Mar 9 04:49:56 EST 2007


---------- Forwarded message ----------
From: James Mead <jamesmead44 at gmail.com>
Date: 07-Mar-2007 10:04
Subject: Re: Mocha raise exception first call, return value second call
To: ruby-talk at ruby-lang.org

On 07/03/07, Raymond O'Connor <nappin713 at yahoo.com> wrote:
>
> Is there a way to have mocha raise an exception the first time an stub
> is called, and then return a value the second time it is called?
>
> First example, I'm trying to connect to a server and test the case where
> my first attempt raises a 'Server busy' exception or something of that
> sort.  So I wait, then retry and then the second time the connection is
> successful.


results = [ lambda { raise ServerBusy }, lambda { return true } ]
object.stubs(:connect).returns { lambda { results.shift.call } }

You might be interested in another recent thread
(http://rubyforge.org/pipermail/mocha-developer/2007-March/000225.html
) which discusses how we might come up with a syntax to make this easier.

-- 
James.
http://blog.floehopper.org

-- 
James.
http://blog.floehopper.org


More information about the mocha-developer mailing list