[mocha-developer] Parameter Matchers with optional params
Andrew Vit
andrew at avit.ca
Mon Sep 24 21:22:24 EDT 2007
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, ¶meter_block)
@parameters, @parameter_block = arguments, parameter_block
class << @parameters; def to_s; join(', '); end; end
Thanks!
--Andrew Vit
More information about the mocha-developer
mailing list