Consider adding jMock custom actions to provide side effect functionality
Reported by James Mead | November 11th, 2010 @ 06:14 PM
Comments and changes to this ticket
-
Alex November 11th, 2010 @ 06:28 PM
Am I wrong, or is the JMock custom action essentially the same thing as this patch?
JMock uses 'will', I used 'runs'.
jmock: oneOf (mangoTree).pickFruit(with(any(Collection.class))); will(addElements(mango1, mango2));
mocha: mangoTree.expects(:pickfruit).runs{addElements(mango1, mango2)}
-
Alex November 11th, 2010 @ 06:30 PM
To add a use case, I mocked an API and was using some test data (tumblr posts: 5 normal, 4 pics, 3 quotes etc.)
To check things were saving as they should, I needed to mock 'save' and update an external counter every time it was called.
-
James Mead November 11th, 2010 @ 06:48 PM
@alex: I thought that one key difference was that you are allowing side effects to change anything, not just parameters passed into the stubbed method.
@eydaimon: Can you post a complete example test and implementation that demonstrates what you are trying to do and I'll see if I can help. It's much easier to discuss concrete code rather than descriptions of code.
-
James Mead November 11th, 2010 @ 06:59 PM
@alex: to better understand your use case, it would be helpful to see the test and the code e.g. in a Gist
-
Alex November 11th, 2010 @ 07:42 PM
Here's some code pulled from my tumblr-sync code.
I wanted to be able to get actual numbers for saves called etc. so that I could ensure my code was processing it's info properly. 'runs' seemed like a good way to approach that, and possible other situations.
-
James Mead November 12th, 2010 @ 11:40 AM
@alex: can you not use the existing Mocha expectation "cardinality" methods e.g. times, at_least, at_most?
I've had a go at an alternative version of your pastie.
-
James Mead November 15th, 2010 @ 10:26 PM
@eydaimon: From what I can tell, you want to know that the LogMail#send method has been called. Is that correct? If so, can you not use the following :-
LogMail.any_instance.expects(:send)
This will give you an error along the following lines if the send method is not called :-
not all expectations were satisfied unsatisfied expectations: - expected exactly once, not yet invoked: #<AnyInstance:LogMail>.send(any_parameters)
An alternative would be to stub the LogMail.new method and return a mock :-
log_mail = mock('log_mail') log_mail.expects(:send) LogMail.stubs(:new).returns(log_mail)
I hope this helps. Cheers, James.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
A mocking & stubbing library for Ruby.
* <a href="http://github.com/floehopper/mocha">GitHub repository</a>
* <a href="http://mocha.rubyforge.org">Documentation</a>
* <a href="http://groups.google.com/group/mocha-developer">Mailing List</a>