[mocha-developer] Is anybody using a block to verify arguments passed to an expectation?

Chris Roos chrisjroos at gmail.com
Tue Oct 10 07:12:07 EDT 2006

For example...

def test_should_receive_at_least_three_arguments
	object = mock
	object.expects(:foo).with { |*args| args.size >= 3 }
	#=> Rather pointless expectation to ensure that we get at least three arguments

If possible, I'd like to remove/rename this functionality.  Instead,
I'd like to be able to set an expectation that checks for specified
arguments _and_ a block.

A simple illustration would be with a method that delegates directly
to another object.

def delegate(delegate, method_sym, *args, &blk)
  delegate.__send__(method_sym, *args, &blk)

I'd like to have the following test that ensures I am correctly
passing all arguments to my delegate object.

def test_should_delegate
	method_sym = :foo
	args = [1, 2]
	blk = lambda {}
	mock_delegate = mock
	mock_delegate.expects(method_sym).with(*args, &blk)
	delegate(mock_delegate, method_sym, *args, &blk)

This currently fails because Expectation#with evaluates the block to
determine whether the expectation is valid or not.

So, is anyone using it?  Any thoughts on what I've set out above,
maybe the test I want to create isn't a very good test?

I appreciate your feedback.


More information about the mocha-developer mailing list