-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
Mocking concrete class that is declared as interface don't use mock any longer #452
Comments
Hi @toha73, Thanks for reporting. I've run a quick check, the relevant change in Moq's behaviour happened back in version 4.7.58. It is very likely due to the following bugfix in 4.7.58 as mentioned in the changelog:
This was a major bug causing tons of issues, the bugfix was therefore very much necessary. The bugfix reverted Moq's behaviour back to how things used to work in previous (pre-bug) versions. That being said, something strange is going on in your code, I'll report back once I've taken a closer look at it. |
@toha73, here's what I found:
I believe the real problem here is that Moq doesn't warn you that you are setting up a
Add the Remember, C# makes methods implicitly Currently, Moq only complains about setups that involve static or non-virtual methods. Your I think Moq could & should be more helpful here and complain (read: throw an exception) in situations like yours, when someone is trying to setup a sealed method that it can't actually intercept. Do you agree? |
Thank you for the effort. I've already fixed the problem in our code by using Yes I agree it would be nice if it was possible to throw an exception. Although, not sure if there are cases when you're supposed be able to set up against a method that isn't sealed. |
Thanks for the quick answer!
I think I'll prepare a PR for this.
I am not sure that I understand. You can only set up methods that aren't sealed, for the simple reason that sealed methods cannot be overridden in a derived class (which is what Moq's mocks really are). |
My error, I meant "... set up against a method that is sealed". |
OK. I'll think there's no valid use case but I'll think about this some more. Thanks again for your valuable feedback! :) |
Let's track this as a bug in a separate issue. |
Noticed this when upgrading from Moq 4.7.10 to 4.7.99.
In one of our test we by mistake declared a variable as an interface but mocked against the concrete implementation. After upgrade it started using the concrete method instead of the mocked one. Maybe this was a bug in former version?
The following code works in version 4.7.10 but fails for 4.7.99:
This was easily fixed by mocking against ITest. But was this a planned change in behaviour?
The text was updated successfully, but these errors were encountered: