Set expectation of method call while still calling original implementation [duplicate]
Asked Answered
D

1

13

It appears as though setting any method-call expectation with Mocha prevent the original implementation from being called.

This seems to cover calling the original method with rspec.

Is there a way to do this with Mocha? Or does anyone know why this wouldn't be supported?

Draftee answered 1/12, 2011 at 19:12 Comment(2)
I've since transitioned to mocking with rspec with great results.Draftee
4 years later and you're not the only one @aceofspades.Impresa
K
-8

This is not possible for a couple reasons. One reason is that testing the side effect of a method and testing the invocation of a method can be done more clearly in two separate tests. Another reason I've read is that in purist unit testing, you wouldn't make an assertion about another method's side effect. You would test the invocation and then test the called methods side effects in unit tests for that specific method.

Kwashiorkor answered 24/1, 2014 at 14:43 Comment(4)
Downvoting: I think you're confusing return values and side effects.Petropavlovsk
@MarnenLaibow-Koser Could you be more specific? I can make a correction if there is a mistake but please remove the downvote.Kwashiorkor
You're talking about making "an assertion about another method's side effect", but what the OP appears to be doing (though I admit it's vague) is trying to rely on the return value of another method. That's quite different from the side effect. As for the downvote, I downvoted because I think your answer was incorrect and misleading (for the reasons already given). I won't remove the downvote unless I no longer believe that to be the case.Petropavlovsk
I agree that the return value and the side effects are two different things. I am in a situation where I need to assert that a given method is called during a test, but I also need it to return the value resulting from the original invocation. Rspec mocks handles this, so I can't see why mocha can't.Greenhaw

© 2022 - 2024 — McMap. All rights reserved.