Beispiel #1
0
        public async Task LoggingServersService_Get_OnServersServiceException_ShouldLogError()
        {
            // arrange
            var mockLogger = new Mock <ILogger>();
            var isCalled   = false;

            mockLogger.Setup(l => l.Error(It.IsAny <string>(), It.IsAny <ServersServiceException>())).Callback(() => isCalled = true);
            var mockServersService = new Mock <IServersService>();

            mockServersService.Setup(s => s.Get(It.IsAny <string>())).ThrowsAsync(new ServersServiceException("Some exception"));
            var loggingServersService = new LoggingServersService(mockServersService.Object, mockLogger.Object);

            // act and assert
            await Assert.ThrowsExceptionAsync <ServersServiceException>(() => loggingServersService.Get(It.IsAny <string>()));

            Assert.IsTrue(isCalled);
        }
Beispiel #2
0
        public async Task LoggingServersService_Get_OnSuccess_ShouldLogInfo2Times()
        {
            // arrange
            var mockLogger = new Mock <ILogger>();
            var callCount  = 0;

            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 <Server[]>())).Callback(() => callCount++);
            var mockServersService = new Mock <IServersService>();

            mockServersService.Setup(s => s.Get(It.IsAny <string>())).ReturnsAsync(new Server[] { new Server {
                                                                                                      Name = "Lietuva"
                                                                                                  } });
            var loggingServersService = new LoggingServersService(mockServersService.Object, mockLogger.Object);

            // act
            var result = await loggingServersService.Get(It.IsAny <string>());

            // assert
            Assert.AreEqual(2, callCount);
        }