public async Task LoggingAuthenticationService_Authentication_OnException_ShouldLogError() { // arrange var mockLogger = new Mock <ILogger>(); var isCalled = false; var credentials = new Credentials { Username = "******", Password = "******" }; mockLogger.Setup(l => l.Error(It.IsAny <string>(), It.IsAny <Exception>())).Callback(() => isCalled = true); var mockAuthenticationService = new Mock <IAuthenticationService>(); mockAuthenticationService.Setup(a => a.Authenticate(It.IsAny <Credentials>())).ThrowsAsync(new Exception("Fatal exception")); var loggingAuthenticationService = new LoggingAuthenticationService(mockAuthenticationService.Object, mockLogger.Object); // act and assert await Assert.ThrowsExceptionAsync <Exception>(() => loggingAuthenticationService.Authenticate(credentials)); Assert.IsTrue(isCalled); }
public async Task LoggingAuthenticationService_Authentication_OnSuccess_ShouldLogInfo2Times() { // arrange var mockLogger = new Mock <ILogger>(); var callCount = 0; var credentials = new Credentials { Username = "******", Password = "******" }; mockLogger.Setup(l => l.Info(It.IsAny <string>(), It.IsAny <string>())).Callback(() => callCount++); mockLogger.Setup(l => l.Info(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Callback(() => callCount++); var mockAuthenticationService = new Mock <IAuthenticationService>(); mockAuthenticationService.Setup(a => a.Authenticate(It.IsAny <Credentials>())).ReturnsAsync("token"); var loggingAuthenticationService = new LoggingAuthenticationService(mockAuthenticationService.Object, mockLogger.Object); // act var result = await loggingAuthenticationService.Authenticate(credentials); // assert Assert.AreEqual(2, callCount); }