adrianiftode / Moq.ILogger

Easy verify ILogger Moq mocks
Apache License 2.0
67 stars 7 forks source link

Asserting log method called 0 times or never doesnt work #5

Closed ruaridhnewman closed 3 years ago

ruaridhnewman commented 3 years ago

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
    }
adrianiftode commented 3 years ago

Thanks for opening this issue. This is now fixed in version 1.1.7

ruaridhnewman commented 3 years ago

Great, thank you!