コード例 #1
0
        public async Task Index_ReturnsErrorView_WhenExceptionIsRaised()
        {
            // Arrange
            var mockService = new Mock <IAccessLogService>();
            var mockLogger  = new Mock <ILogger <AccessLogController> >();

            mockService.Setup(service => service.ListAccessLogsAsync(It.IsAny <string>(), It.IsAny <string>()))
            .Throws(new Exception());

            var controller = new AccessLogController(mockService.Object, mockLogger.Object);

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <ErrorViewModel>(viewResult.ViewData.Model);
        }
コード例 #2
0
        public async Task Index_ReturnsViewResult_WithListOfAccessLogs()
        {
            // Arrange
            var logs = await _context.AccessLogs.ToListAsync();

            // use the sample data from the dummy _context InMemoryDatabase
            var mockService = new Mock <IAccessLogService>();

            mockService.Setup(service => service.ListAccessLogsAsync(It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(logs);

            var controller = new AccessLogController(mockService.Object, null);

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <IEnumerable <AccessLog> >(viewResult.ViewData.Model);

            Assert.Equal(3, (model as List <AccessLog>).Count);
        }