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.
Cheers!
Ray
···
--
Posted via http://www.ruby-forum.com/.
server.expects(:connect).returns do
server.expects(:connect).returns(true)
raise Busy
end
Hackish? Unclean? 
jeremy
···
On 3/6/07, Raymond O'Connor <nappin713@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.
···
On 07/03/07, Raymond O'Connor <nappin713@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.
--
James.
Thanks for the help both of you! I ended up using the method James
proposed (it was cleaner
).
Very interesting thread too. From a usability standpoint, adding a
raises method to TestCase seems to provide the most intuitive syntax...
at least for me it does. Can't wait to see how things pan out!
···
--
Posted via http://www.ruby-forum.com/.