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

IMemoryCache mock error #446

Closed
michaelbowman1024-zz opened this issue Sep 20, 2017 · 4 comments
Closed

IMemoryCache mock error #446

michaelbowman1024-zz opened this issue Sep 20, 2017 · 4 comments

Comments

@michaelbowman1024-zz
Copy link

michaelbowman1024-zz commented Sep 20, 2017

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:

[Fact]
public void SearchProducts()
{
    //Arrange
    var memoryCacheMock = new Mock<IMemoryCache>();
    memoryCacheMock.Setup(m => m.Set(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MemoryCacheEntryOptions>());

    //Some other test code
}
@stakx
Copy link
Contributor

stakx commented Sep 20, 2017

Like the exception message says: You cannot do setups for extension methods. They are static methods after all and as such cannot be intercepted.

@stakx stakx closed this as completed Sep 20, 2017
@michaelbowman1024-zz
Copy link
Author

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?

@stakx
Copy link
Contributor

stakx commented Sep 21, 2017

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

@tiagocrizanto
Copy link

tiagocrizanto commented Mar 31, 2022

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>);

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

No branches or pull requests

3 participants