-
-
Notifications
You must be signed in to change notification settings - Fork 811
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
Moq does not mock 'Explicit Interface Implementation' and 'protected virtual' correctly #657
Comments
Hi @oddbear, thanks for reporting this. It's not the first time I've seen an issue about Moq picking the wrong method when Just as an aside:
You can mock protected methods with Moq, using I'll post back later about the actual issue here. |
Great thanks. |
@oddbear, this is a fixable bug in In the above code,
As you can see in that code, Moq performs a rather imprecise comparison to match the invoked method against a setup (but it's enough in all but a few corner cases such as yours). We'd need to add an additional check to prevent a match if the invoked method is not in fact the one that implements the set-up interface method. The method shown above is one that gets called very frequently, so it's important to consider performance for any bug fix. I have a naïve bug fix that's probably way too expensive; I'll need to think some more about how to make the additional check cheaper. |
The PR I've submitted contains the "naïve" bug fix mentioned above. While it's functional, it's also inefficent. A more efficient bug fix will become possible once we've dealt with #643, as that will enable us to map interface methods to class methods at setup time (vs. only being able to do this accurately at invocation time, as it is today), meaning that this mapping can happen only a single time per setup, instead of possibly for every invocation. That's the reason why I've rescheduled this to the 4.10.0 milestone (which we likely won't reach before late autumn). @oddbear, I hope that's fine with you. |
That's great. Ty. |
Note to self, now that #765 is merged, we might be able to proceed with this issue. |
If we use Explicit Interface Implementation, with same name as a protected virtual method, Moq will mock the wrong method.
Protected methods should not be possible to mock, due to their protection level.
We found this issue in one of our Custom HttpHandlers that inherits from HttpClientHandler.
The method we implement is a overridden protected virtual method SendAsync. To be able to mock this, we used a Interface method as a layer in between for calling base (do an actual httprequest instead for just calling the mock).
Steps to Reproduce
Code example of code with this behaviour:
Working code:
Expected Behavior
It should write out "1" as i am overriding the interfaced method and not the virtual one.
The virtual method should not be mocked in any circumstances, as this one is protected.
Actual Behavior
It writes out "3" since it mocks the protected virtual method, and not the interfaced method.
The text was updated successfully, but these errors were encountered: