-
-
Notifications
You must be signed in to change notification settings - Fork 813
Mocking Static Abstract Interface Methods? #1238
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
Comments
I don't know for sure, but I suspect not. DynamicProxy still sees interfaces the way they were pre-.NET 5, and doesn't yet support all their new capabilities. Because Moq is based on DynamicProxy, it shares those same limitations... at least for now. Support for non-public, non-abstract, non-instance interface members would have to be added in DynamicProxy. Unfortunately, development (or, more precisely, the release cycle) over there has pretty much come to a halt. So I am not very optimistic that we'll be able to support your scenario anytime soon. |
Just thought I would add that Moq will just error at runtime if you have strict behaviour on and the type you are mocking has a static abstract property on it even if you do not touch that property with Moq. However if you do not need to mock that property and just want to ignore it you can implement the interface in an abstract class, implement the static properties in the abstract class and then it works like any other abstract class. Not a work around if you need to Moq the static properties, but helps at least ignore them and still use Moq so could help someone. |
It looks like the only viable solution, as of .NET 7, would require sacrificing strong typing. Per compiler error So any kind of solution where you mock this kind of interface method would have to use You could get around that by extending the interface with a default implementation that throws.. but there's no Expression syntax for static type method calls.. so you'd need Sad :( |
There's a new feature currently in preview which allows you to add
static abstract
methods to an interface:https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/static-abstract-interface-methods
Is there a way to add a mock behavior for such a method, using moq?
I realize there may not be a "pretty" way to do it yet, but I would like to know if there is any way to do it? I'm used to the
Setup
,SetupGet
, etc. methods, but it doesn't appear there is one for static abstract methods yet.The text was updated successfully, but these errors were encountered: