[mocha-developer] Beyond multiple return values

Frederick Cheung fred at 82ask.com
Sat Mar 3 11:06:58 EST 2007

First off, I love mocha and have been using it all over the place  
ever since  I found it a few months ago.

So I noticed the other day rather belatedly that mocha-0.4.0 had been  
released and that we can now do the object.stubs(:method).returns 
(:first_value, :second_value). Much neater than fiddling with lambdas  
everytime this sort of behaviour is needed

But can we go further ? I was writing a test today and I wanted the  
first call to a method to raise an exception and the next call to  
return a value (I was testing a method on an ActiveRecord object  
where if save! raises StaleObjectError then we reload and retry in  
the proper way, the second call to save! shouldn't raise.).

The same basic method works:

lambdas = [lambda {raise 'Foo'}, lambda {return true}]
object.stubs(:save!).returns {lambda {lambdas.shift.call}}

It's a lot less readable than your average lovely bit of mocha based  

Can anyone think of a way of doing this that looks nice?


More information about the mocha-developer mailing list