[mocha-developer] Beyond multiple return values

James Mead jamesmead44 at gmail.com
Mon Mar 5 06:06:45 EST 2007


On 03/03/07, Frederick Cheung <fred at 82ask.com> wrote:
>
> First off, I love mocha and have been using it all over the place
> ever since  I found it a few months ago.


Good stuff.

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
> test.
>
> Can anyone think of a way of doing this that looks nice?
>

Interesting. Perhaps we can at least do this...

object.stubs(:save!).returns(lambda { raise 'Foo' }, true )

Or (if we add a 'raises' method to TestCase)...

object.stubs(:save!).returns(raises(), true)

Thoughts anyone?

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


More information about the mocha-developer mailing list