Skip to content
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 Static Abstract Interface Methods? #1238

Closed
the-avid-engineer opened this issue Mar 2, 2022 · 3 comments
Closed

Mocking Static Abstract Interface Methods? #1238

the-avid-engineer opened this issue Mar 2, 2022 · 3 comments

Comments

@the-avid-engineer
Copy link

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.

@stakx
Copy link
Contributor

stakx commented Mar 12, 2022

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 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.

@crash-dive
Copy link

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.

@the-avid-engineer
Copy link
Author

the-avid-engineer commented Nov 29, 2022

It looks like the only viable solution, as of .NET 7, would require sacrificing strong typing.

Per compiler error CS8920, interfaces that define static abstract methods cannot be used as generic type arguments unless they implement the static abstract methods.

So any kind of solution where you mock this kind of interface method would have to use typeof(IMyInterface) in some way, because new Mock<IMyInterface>() will throw compiler error CS8920.

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 nameof(IMyInterface.StaticAbstractMethod)

Sad :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants