[mocha-developer] foo.expects(:blah).returns(10).then(11) syntax

Jay jay at jayfields.com
Mon Sep 25 08:05:33 EDT 2006


Yes, sorry.  I should have checked out trunk before sending you  
something :)

I do like the fluent interface that Mocha provides.  I was trying to  
think of something more readable and would allow you to return values  
and then raise.  I considered then_return and then_raise, but in the  
end neither seemed better than simply providing a comma delimited list.

Anyway, thanks for the response.
Jay

On Sep 24, 2006, at 5:17 PM, James Mead wrote:

> On 22/09/06, Jay <jay at jayfields.com> wrote:
> I recently needed the behavior of my object to change on subsequent
> method calls so I added the syntax above.
>
> Here's the diff of changes I made:
>
> Index: test/mocha/expectation_test.rb
> ===================================================================
> --- test/mocha/expectation_test.rb      (revision 854)
> +++ test/mocha/expectation_test.rb      (working copy)
> @@ -95,6 +95,12 @@
>       assert_equal 99, expectation.invoke
>     end
>
> +  def test_should_return_values_in_order
> +    expectation = Expectation.new(:expected_method).returns(9).then 
> (10)
> +    assert_equal 9, expectation.invoke
> +    assert_equal 10, expectation.invoke
> +  end
> +
>     def test_should_return_nil_if_no_value_specified
>       expectation = Expectation.new(:expected_method)
>       assert_nil expectation.invoke
> Index: lib/mocha/expectation.rb
> ===================================================================
> --- lib/mocha/expectation.rb    (revision 854)
> +++ lib/mocha/expectation.rb    (working copy)
> @@ -21,7 +21,7 @@
>         @method_name = method_name
>         @count = 1
>         @parameters, @parameter_block = AlwaysEqual.new, nil
> -      @invoked, @return_value = 0, nil
> +      @invoked = 0
>         @backtrace = backtrace || caller
>       end
>
> @@ -69,19 +69,29 @@
>       end
>       def returns(value)
> -      @return_value = value
> +      return_value << value
>         self
>       end
> +
> +    def then(value)
> +      return_value << value
> +      self
> +    end
> +
> +    def return_value
> +      @return_value ||= []
> +    end
>
>       def raises(exception = RuntimeError, message = nil)
> -      @return_value = lambda{ raise exception, message }
> +      return_value << lambda{ raise exception, message }
>         self
>       end
>       def invoke
>         @invoked += 1
>         yield(*@parameters_to_yield) if yield? and block_given?
> -      @return_value.is_a?(Proc) ? @return_value.call : @return_value
> +      this_return = return_value.size > 1 ? return_value.shift :
> return_value.first
> +      this_return.is_a?(Proc) ? this_return.call : this_return
>       end
>       def verify
>
> Hi Jay,
>
> Thanks for your patch and in particular for including a test.  
> Interestingly I recently made a change (revision #55) to allow  
> consecutive return values to be specified like this...
>
>   object = mock()
>   object.expects(:expected_method).returns 
> (:result1, :result2, :result3)
>
>   object.expected_method # => :result1
>   object.expected_method # => :result2
>   object.expected_method # => :result3
>   object.expected_method # => nil
>
> I wonder if this would be sufficient for your needs. Let me know  
> what you think.
>
> I hope to do a release in the next few days.
>
> Thanks.
> -- 
> James.
> http://blog.floehopper.org
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/mocha-developer/attachments/20060925/b8446a50/attachment-0001.html 


More information about the mocha-developer mailing list