public override void ExecuteJob() { _logger.Info("Starting execution of Custom Job"); var result = _someService.DoSomething(); _logger.Debug($"The result of execution: {result}"); _logger.Info("The execution has been completed successfully"); }
public void LogsErrorWhenInputIsZero() { var mockLogger = new Mock <ILogger <SomeService> >(); var someService = new SomeService(mockLogger.Object); someService.DoSomething(0); // Option 1: Try to verify the actual code that was called. // Doesn't work. mockLogger.Verify(l => l.LogError(It.IsAny <Exception>(), It.IsAny <string>(), 0)); }
public void LogsErrorWhenInputIsZeroTake3() { var fakeLogger = new FakeLogger(); var someService = new SomeService(fakeLogger); someService.DoSomething(0); // Option 3: Create your own instance of ILogger<T> that has a non-extension version of the method // Doesn't work, unless you change system under test to take in a FakeLogger (which is useless) Assert.NotNull(FakeLogger.ProvidedException); Assert.NotNull(FakeLogger.ProvidedMessage); }
public void LogsErrorWhenInputIsZeroTake2() { var mockLogger = new Mock <ILogger <SomeService> >(); var someService = new SomeService(mockLogger.Object); someService.DoSomething(0); // Option 2: Look up what instance method the extension method actually calls: // https://github.com/aspnet/Logging/blob/dev/src/Microsoft.Extensions.Logging.Abstractions/LoggerExtensions.cs#L342 // Mock the underlying call instead. // Works but is ugly and brittle mockLogger.Verify(l => l.Log(LogLevel.Error, 0, It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >())); }
public override string ToString() { return(_service.DoSomething(_value)); }
public string Get(int id) { _someService.DoSomething(id); return("value"); }