-
-
Notifications
You must be signed in to change notification settings - Fork 813
IMemoryCache mock error #446
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
Like the exception message says: You cannot do setups for extension methods. They are static methods after all and as such cannot be intercepted. |
Ok, that gives an answer to the error message source. Let's go above and beyond the bare minimum here. What would be the expectation for someone to both use caching AND perform unit testing? |
@michaelbowman1024, this is not the best place to ask pure usage questions: The focus here is to track issues with Moq (bugs, feature proposals, etc.). May I suggest that you ask your usage-related question e.g. on Stack Overflow? That being said, remember that you're dealing with an extension method. In all likelihood, it is implemented in terms of the actual interface methods which can be intercepted by Moq, so I suggest you take a look at the extension method's source code to find out which interface method(s) it decorates, then setup that instead. |
Well I reach this page some times and can't find an anwser. So here are my 0,05 cents of contribution. Extension methods are actually static methods and they cannot be mocked using moq (as @stakx explained), so I find this way to mock the Set of IMemoryCache with Moq memoryCacheMock
.Setup(x => x.CreateEntry(It.IsAny<object>()))
.Returns(Mock.Of<ICacheEntry>); |
When trying to perform a setup to call Set on an instance of MemoryCache, I receive the following error:
System.NotSupportedException : Invalid setup on an extension method: m => m.Set(It.IsAny(), It.IsAny(), It.IsAny())
at Moq.Mock.ThrowIfSetupExpressionInvolvesUnsupportedMember(Expression setup, MethodInfo method) in C:\projects\moq4\Source\Mock.cs:line 812
at Moq.Mock.<>c__DisplayClass61_0`2.b__0() in C:\projects\moq4\Source\Mock.cs:line 465
Here's the test in question:
The text was updated successfully, but these errors were encountered: