[mocha-developer] Parameter Matchers with optional params

Zach Moazeni zach.lists at gmail.com
Mon Sep 24 21:51:37 EDT 2007


Actually,

I tend to use a more readable format when I do use the block format:

object = mock()
object.expects(:expected_method).with do | first_param, second_param |
   ...
end

Note - As I work with mocking more, I've noticed my dependence on the  
block matcher weaken considerably.

-Zach

On Sep 24, 2007, at 9:22 PM, Andrew Vit wrote:

> On Sep 24, 2007, at 3:11 AM, James Mead wrote:
>
>> For the moment, I think you should be able to use the
>> "parameter_block"
>> version of Expectation#with (
>> http://mocha.rubyforge.org/classes/Mocha/Expectation.html#M000024).
>>
>> Please let me know if you think the documentation could be improved.
>
> That's great James. I rather like this way of matching parameters,
> much like Enumerable#detect... You're confident that the
> ParameterMatchers will be versatile enough to do away with the block?
>
> The only point that wasn't fully clear in the docs is how it can work
> with multiple args... It's probably obvious to anyone who's worked
> with ruby blocks for a while, but I was getting the warning at first
> until I figured out how it works...
>
> Here's a diff suggestion for your docs:
>
>
> --- expectation.rb (saved version)
> +++ (current document)
> @@ -212,6 +212,17 @@
>       #   object.expects(:expected_method).with() { |value| value % 4
> == 0 }
>       #   object.expected_method(17)
>       #   # => verify fails
> +    #
> +    # Multiple parameters can be received as an array.
> +    # Note the use of a splat (asterisk) character on the
> +    # block parameter in this usage:
> +    #   object = mock()
> +    #   object.expects(:expected_method).with() do |*args|
> +    #     args[0] % 4 == 0 && args[1] == 29
> +    #   end
> +    #   object.expected_method( 2008, 29 )
> +    #   => verify succeeds
> +    #
>       def with(*arguments, &parameter_block)
>         @parameters, @parameter_block = arguments, parameter_block
>         class << @parameters; def to_s; join(', '); end; end
>
>
> Thanks!
> --Andrew Vit
>
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer



More information about the mocha-developer mailing list