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

Error message when attempting to mock an extension method #317

Closed
gregsdennis opened this issue Jan 8, 2017 · 3 comments
Closed

Error message when attempting to mock an extension method #317

gregsdennis opened this issue Jan 8, 2017 · 3 comments

Comments

@gregsdennis
Copy link

I ran into an issue when I was (unwittingly) attempting to mock an extension method. I've seen #189, and I understand why I can't do that. Still, I think the error messaging could be better.

I have an interface:

public interface ILogger
{
    void Error(string);
}

and an extension method:

public void Error(this ILogger logger, Exception e)
{
    ...
}

and a test

[TestMethod]
public void TestIt()
{
	var logger = new Mock<ILogger>();
	logger.Verify(l => l.Error(It.IsAny<Exception>()));
}

As expected, the test fails. But why the test fails is confusing. The following error is returned:

Invalid verify on a non-virtual (overridable in VB) member: l => l.Error(It.IsAny())

The error says nothing about how Error(Exception) isn't even a member of ILogger.

@aKabirKhan
Copy link

What did you end up doing to mock extension methods. I need to mock app.UseMvc(); method in Configure method of the start up class but I cannot do so.

@gregsdennis
Copy link
Author

I verified what the extension method did, which was to call a different method on the logger.

@stakx
Copy link
Contributor

stakx commented Jul 2, 2017

This will be fixed in the next release of Moq. The exception message will read:

Invalid verify on an extension method: l => l.Error(It.IsAny())

@stakx stakx closed this as completed Jul 2, 2017
@devlooped devlooped deleted a comment from darraghjones Aug 30, 2018
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

4 participants