I have mock of Serilog.ILogger
, and I want to verify that no overloads of logger.Error(...)
was called. There are currently 10 overloads, so I would want to avoid setting up every one of them. The test would also break in the future if they added more overloads.
So, instead of this:
var loggerMockSetup = new Mock<Serilog.ILogger>(MockBehavior.Loose);
loggerMockSetup.Verify(x => x.Error(It.IsAny<String>()), Times.Never);
loggerMockSetup.Verify(x => x.Error(It.IsAny<String>(), It.IsAny<Object[]>()), Times.Never);
loggerMockSetup.Verify(x => x.Error(It.IsAny<Exception>(), It.IsAny<String>()), Times.Never);
// ... and so on for all the overloads
Can I do something like the following?
var loggerMockSetup = new Mock<Serilog.ILogger>(MockBehavior.Loose);
loggerMockSetup.VerifyAllOverloads(x => x.Error, Times.Never);
It has to be specific to all overloads to x.Error(...)
and not all methods on the object, because I have unrelated calls to e.g. x.Information(...)
.
CodePudding user response:
I see the following options:
- Create a custom extension method
VerifyAllOverloads
→ less work, but needs to be modified as soon as new overloads are added toILogger
- Create a custom implementation of
ILogger
(e. g.UnitTestLogger
) which forwards all overload calls to a common method. Then you can react onto this common method → less work, but needs to be modified as soon as new overloads are added toILogger
- Use reflection/source generators to fetch all overloads of
ILogger
and build a custom assertion based on this → more work, but doesn't need to be modified as soon as new overloads are added toILogger