Verifying method call with any struct parameter in Moq

Changing It.IsAny<Func<object, Exception, string>>()) to (Func<object, Exception, string>) It.IsAny<object>() seems to solve the problem. object can also be replace by IsAnyType if you're on Moq 4.13+.

Internally the Logger class uses FormattedLogValues as the state parameter (the object in my example). The change to struct seems to have something to do with it. What exactly the cause is I'm not sure. But there seems to be an issue on the Moq GitHub repo describing a few more details. There doesn't seem to be a concrete explanation yet why it used to work, but more info will probably be posted there soon.

https://github.com/moq/moq4/issues/918


I found the same problem in Github.

I made an extension method for the solution:

public static void VerifyLog<T>(this Mock<ILogger<T>> mockLogger, Func<Times> times)
{
    mockLogger.Verify(x => x.Log(
        It.IsAny<LogLevel>(),
        It.IsAny<EventId>(),
        It.Is<It.IsAnyType>((v, t) => true),
        It.IsAny<Exception>(),
        It.Is<Func<It.IsAnyType, Exception, string>>((v, t) => true)), times);
}