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

Resolve ILogger from a logger factory #126

Open
adamhewitt627 opened this issue Sep 1, 2021 · 5 comments
Open

Resolve ILogger from a logger factory #126

adamhewitt627 opened this issue Sep 1, 2021 · 5 comments

Comments

@adamhewitt627
Copy link
Collaborator

I'm thinking of something like this being added to the built-in resolvers.

private sealed class AutoMockerLoggerResolver : IMockResolver
{
    public void Resolve(MockResolutionContext context)
    {
        if (context.Value is not null) return;

        Type type = context.RequestType;

        if (type == typeof(ILogger))
        {
            ILoggerFactory factory = context.AutoMocker.Get<ILoggerFactory>();
            context.Value = factory.CreateLogger(string.Empty);
        }
        else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ILogger<>))
        {
            ILoggerFactory factory = context.AutoMocker.Get<ILoggerFactory>();

            context.Value = typeof(AutoMockerLoggerResolver)
                .GetMethod(nameof(CreateLogger), BindingFlags.Static | BindingFlags.NonPublic)
                .MakeGenericMethod(type.GenericTypeArguments[0])
                .Invoke(null, new object[] { factory });
        }
    }

    private static ILogger<T> CreateLogger<T>(ILoggerFactory factory)
        => factory.CreateLogger<T>();
}
@304NotModified
Copy link

304NotModified commented Oct 20, 2021

That would be great! Or make it opt-in like this for example:

  mocker.AddLoggerMocks();

@adamhewitt627
Copy link
Collaborator Author

I have no idea why I suggested this. What would the benefit be to get a logger this way rather than just the mock?

@304NotModified
Copy link

304NotModified commented Feb 19, 2022

Maybe its an issue that the loggermock returns null for the methods (e.g. beginscope) - 》crashes.

Note: Most of the time there is no need to assert the loggermock.

Personally I create a (generic) spy when we need to assert the logger. The ilogger is hard to verify due to the extensions and conversions.

@MelGrubb
Copy link

Well, I wish this wasn't closed. It's the closest thing I've found to what I'm trying to do. I want to NOT mock ILogger. I want to call Mocker.Use and register the open generic logger type with a lambda so that it uses an existing LoggerFactory to create a real logger to be injected during my tests at runtime. Why? So that I can see what happened during the test. I want to call CreateInstance to get my system under test, and I'd like it to use a real Logger. I just want a callback/lambda to specify it so that I can resolve the generic at runtime.

@bamajap
Copy link

bamajap commented May 18, 2023

Well, I wish this wasn't closed. It's the closest thing I've found to what I'm trying to do. I want to NOT mock ILogger. I want to call Mocker.Use and register the open generic logger type with a lambda so that it uses an existing LoggerFactory to create a real logger to be injected during my tests at runtime. Why? So that I can see what happened during the test. I want to call CreateInstance to get my system under test, and I'd like it to use a real Logger. I just want a callback/lambda to specify it so that I can resolve the generic at runtime.

I think I understand what you're describing b/c I do something similar by setting up a console logger for my unit tests. This may work for your use-case.

using Microsoft.Extensions.Logging;
...

var mocker = new AutoMocker(MockBehavior.Strict);
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger<ClassUnderTest>();
mocker.Use(logger);

var sut = mocker.CreateInstance<ClassUnderTest>();
...

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

4 participants