When trying to assert that the LogError method wasn't called using Times.Never or Times.Exactly(0), the result is always:
Expected invocation on the mock at least once, but was never performed
When I set the Times field to Times.Exactly(2) (for example), I get the result:
Expected invocation on the mock exactly 2 times, but was 0 times
My test is set up as follows:
// sut
public async Task<bool> Method() {
var result = await _anotherService.DoSomething(); // this returns true/false
if (!result) {
_logger.LogError("An error");
}
return result;
}
// tests
[Fact]
public async void UseMethod_Success()
{
// Act
var result = await _sut.Method();
// Assert
Assert.True(result);
_logger.VerifyLog(x => x.LogError(It.IsAny<string>()), Times.Never); // Always expects it to be called once, even though `Times.Never` is set
}
When trying to assert that the
LogError
method wasn't called usingTimes.Never
orTimes.Exactly(0)
, the result is always:When I set the
Times
field toTimes.Exactly(2)
(for example), I get the result:My test is set up as follows: