I have my registration and constructor setup to use logging via Microsoft.Extensions.Logging as follows,
public MyService(ILogger<MyService> logger) {}
What I'd like to do, though, is use services.AddSingleton()
in my Startup.cs to avoid using constructor injection. The reason is that it will otherwise require modifying all unit tests to Mock the logger when testing the service.
I believe there's a way to register the specific instance of ILogger in the ServiceCollection, but I cannot sort out the syntax required.
Thanks!
CodePudding user response:
- In unit tests you should not be using a DI container.
- If you were, then you're writing an integration test, not a unit test.
- In unit tests, generally speaking, the arrange part of the test should set-up your SUT by directly invoking SUT's constructor and passing-in test/mock/stub versions of its dependencies.
- In unit tests, ideally you should supply an entire testing-implementation of the entire
Microsoft.Extensions.Logging.ILoggerProvider
interface (and related classes) which will allow you to assert that specific log output messages were written, or to redirectILogger
andILogger<T>
output to your test's own output.- Unfortunately this isn't in-box (and Microsoft won't release their own), but there are many popular libraries for this:
- Alternatively, if your tests aren't concerned with asserting
ILogger
usage and/or if you don't want your SUT's own log output written to your test output, then useNullLoggerFactory
orNullLogger<T>.Instance
in your arrange section to create a stubILogger<T>
that doesn't do anything.
Like this:
using Microsoft.Extensions.Logging.Abstractions;
// [...]
[Fact]
public void MyService_should_do_something()
{
// Arrange:
IArbitraryDependency dep = new StubArbitraryDependency();
#if FULL_MOON_TONIGHT
ILogger<MyService> nullLogger = NullLoggerFactory.Instance.CreateLogger<MyService>();
#else
ILogger<MyService> nullLogger = NullLogger<MyService>.Instance();
#endif
MyService systemUnderTest = new MyService(
arbitraryDependency: dep,
logger : nullLogger
);
// Act:
systemUnderTest.DoSomething();
// Assert:
// [...]
}