Chris Roos
2006-Oct-10 11:12 UTC
[mocha-developer] Is anybody using a block to verify arguments passed to an expectation?
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
end
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)
end
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)
end
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.
Chris
Maybe Matching Threads
- counter-intuitive behaveour when passing a proc to Mocha::Expectation#returns
- Fwd: [PATCH] Remove warning in expectation.rb
- Problem stubbing instances referred to by constants
- small suggestion - once method on Expectation
- Mocking Time, delegating to original object
