Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verfying an error was logged fails #16

Open
menaheme opened this issue Sep 15, 2024 · 4 comments
Open

Verfying an error was logged fails #16

menaheme opened this issue Sep 15, 2024 · 4 comments

Comments

@menaheme
Copy link

Hi
Thank you for this helpful project. I have been using it for a while, and the experience is very good.

Recently written a test that is supposed to make sure an error is logged when a conditions is met.

The error appears to be logged (stepping through with the debugger i see it) but VerifyLog(..) fails the test anyway.

The call to the log looks like this:
private void Alert() { logger.LogError(50100, "the description of the error goes here"); }

and the verifylog assertion looks like this

Mock.Get(logger).VerifyLog(l => l.LogError(50100, It.IsAny<string>()), Times.Once(), "an error must be logged");

the test output is strange:

Message: 
Moq.VerifyLogException : an error must be logged
Expected invocation on the mock once, but was 0 times: l => l.LogError((EventId)50100, It.IsAny(), new[] { })

---- Moq.MockException : an error must be logged
Expected invocation on the mock once, but was 0 times: logger => logger.Log<It.IsAnyType>(LogLevel.Error, 50100, It.Is<It.IsAnyType>((v, t) => True), It.IsAny(), (Func<It.IsAnyType, Exception, string>)It.IsAny())

Performed invocations:

MockILogger:2 (logger):

  ILogger.Log<FormattedLogValues>(LogLevel.Information, 0, KeepAliveMonitor started., null, Func<FormattedLogValues, Exception, string>)
  ILogger.Log<FormattedLogValues>(LogLevel.Error, 50100, the description of the error goes here, null, Func<FormattedLogValues, Exception, string>)

Stack Trace: 
VerifyLogExtensions.Verify[T](Mock1 loggerMock, Expression1 expression, Nullable1 times, Func1 timesFunc, String failMessage)
VerifyLogExtensions.VerifyLog(Mock1 loggerMock, Expression1 expression, Times times, String failMessage)
KeepAliveMonitorTest.Test_Alert_Fired(Int32 i) line 45
InvokeStub_KeepAliveMonitorTest.Test_Alert_Fired(Object, Span1) MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) ----- Inner Stack Trace ----- Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage) line 332 Mock1.Verify(Expression1 expression, Times times, String failMessage) line 770 VerifyLogExtensions.Verify[T](Mock1 loggerMock, Expression1 expression, Nullable1 times, Func`1 timesFunc, String failMessage)

why is the assertion failing the test ?

@adrianiftode
Copy link
Owner

Hi, thanks for logging the issue.

I am trying to reproduce in the following PR, but I am not able to do so.

The only difference I see between your output and the output of a failing test in that branch is the concrete type used by the ILogger generic one. In your case is FormattedLogValues which is a bit odd. Possible to provide more context, maybe the setupt of the Mock?

@menaheme
Copy link
Author

menaheme commented Sep 16, 2024 via email

@menaheme
Copy link
Author

menaheme commented Sep 17, 2024

firs of all , i am using the Microsoft provided dependency injection library in testing (too).

so, to create a logger that will be a Mock<> i defined simple a LoggingProvider class .

using Microsoft.Extensions.Logging;
using Moq;

namespace MarginSpiderTests
{
public class TestLoggingProvider : ILoggerProvider
{
private readonly ILogger mockLogger;

public TestLoggingProvider()

{ mockLogger = Mock.Of<ILogger>(); }
public ILogger CreateLogger(string categoryName)

{ return mockLogger; }
public void Dispose() { }
}
}

and plug it in the DI like this
hostBuilder.Services.AddSingleton<ILoggerProvider, TestLoggingProvider>();

and the test method looks like this:

[Fact]
public void Test_Alert_Not_Fired()
{
var keepAliveMonitor = host.Services.GetRequiredService<KeepAliveMonitor>(); 
var logger = host.Services.GetRequiredService<ILoggerProvider>().CreateLogger(nameof(KeepAliveMonitorTest)); 
keepAliveMonitor.StartMonitoring(0, 120);
Mock.Get(logger).VerifyLog(l => l.LogError(50100, It.IsAny<string>()), Times.Once(), "an error should be logged"); 
}

@adrianiftode
Copy link
Owner

Do you have tests that pass?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants