-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Argument Matchers are being reused across mocks #36
Comments
It is an interesting scenario, and should not be hard to fix. I will look into it after the weekend. |
So I wasn't able to reproduce this behavior. |
I see that I copied the wrong sample, sorry about that, here it is the one that failed:
Interfaces
These are totally made up interfaces for the sake of testing your library, so that's why the names are like A and X. |
Thanks, can you also provide an implementation of |
Yes, good point.
This is simulating the case when you upsert some amount of data and then increment a counter for audit (which is a fairly common case) |
It is reproducible now, thanks. |
I see, my mistake when identifying the issue (I read this tons of times and still I didn't realize of the missing mockio.When()) Thanks for the reply :) |
You made a good point though, I will create a fix that will fail the test with correct error in such cases |
The issue is a bit difficult to spot, but the problem is that Argument Matchers are being re-used across mocks, so if you define two mocks of two different interfaces in the same test with different matchers (e.g.: first AnyString() and second AnyInt) the AnyInt matcher will be used to verify the calls of the first Mock instance.
I've debugged the code and found that the reason behind this issue is that the matchers are assigned to the Thread State when the When methods are invoked BUT they are not bound to the mock being "mocked" but rather to the thread state.
So when the invocation of mockio.AnyInt() happens it is overriding the matcher defined just right above.
I think that the solution should be to bind the matchers to the Handler itself when the mockio.AnyString() is invoked, rather than saving it into a thread scope.
The text was updated successfully, but these errors were encountered: