Consider adding jMock custom actions to provide side effect functionality
-
Alex
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
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.
-
eydaimon
Reposting here from github:
Fopper,
This below is/was my use-case which was solved by Alex's branch:
I just want to make sure a method gets called on an instance. So let's say I have method foo: def foo; o = Object.new; o.some_meth; return o; end # how can I check if some_meth was called on o?
foo() in my case was called deep in some code which I had no access to directly, so I could not rely on any other side-effects to check that it was called. How would I have handled this using mocha as it stands?
-
James Mead
@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
@alex: to better understand your use case, it would be helpful to see the test and the code e.g. in a Gist
-
Alex
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
@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.
-
eydaimon
I believe this should sufficiently describe it:
https://gist.github.com/fdb0907fcae297006f92
I am using Alex's code here. Not sure how I would proceed otherwise. Since i'm creating a new instance of LogMail, I can't set a flag on it to mark it sent.
-
eydaimon
Any insight?
-
James Mead
@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.
-
James Mead
@alex: Did my suggestion help...?
-
eydaimon
That does help. thank you much 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.