Moq and Microsoft.Extensions.Logging.ILogger unit tests failing after Microsoft.Extensions.Logging.Abstractions update

  • Thread starter Thread starter Dudeman 3000
  • Start date Start date
D

Dudeman 3000

Guest
Hello,

Some of my unit tests are failing after updating Microsoft.Extensions.Logging.Abstractions from Version=2.0.0.0 to Version=3.1.1.0.

I have an ILogger mocked as so:

var loggerMock = new Mock<ILogger<BillingEventProcessor>>();
loggerMock.Setup(l => l.Log(
It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.IsAny<IReadOnlyList<KeyValuePair<string, object>>>(),
It.IsAny<Exception>(),
It.IsAny<Func<object, Exception, string>>()));


And a unit that verifies a call to Log() has been made:

logger.Verify(l => l.Log(
It.IsAny<LogLevel>(),
It.IsAny<EventId>(),
It.IsAny<IReadOnlyList<KeyValuePair<string, object>>>(),
It.IsAny<Exception>(),
It.IsAny<Func<object, Exception, string>>())
);


Prior to updating, the IReadOnlyList<KeyValuePair<string, object>> type was of type FormattedLogValues.

In v2, FormattedLogValues was a public class but in v3.1.1, FormattedLogValues is an internal readonly struct. This changes appears to be the source of the test failure.

I've tried using It.IsAny<object> for that 3rd parameter instead of the IReadOnlyList or FormattedLogValues, but I didn't have any luck with that.

Anyone know how I can alter this test code to make the test pass? I can see from debugging info that a call to the Log method is made as expected, I just can't figure out how to set up these mocks correctly with the internal readonly struct parameter.

This is a .NET Core 2.2 project.

Continue reading...
 
Back
Top