コード例 #1
0
        public void GetNextQuestion_WhenCommandReturned_ReturnsOkResultWithCommand()
        {
            // Arrange
            var commandStub = new QuizFlowCommandFinishContract();
            var quiz        = new Quiz();

            var mockQuizRepo = new Mock <IQuizRepository>();

            mockQuizRepo.Setup(repo => repo.GetByID(It.IsAny <int>()))
            .Returns(value: quiz);

            var mockUow = new Mock <IUnitOfWork>();

            mockUow.Setup(uow => uow.QuizRepository)
            .Returns(mockQuizRepo.Object);

            var mockManager = new Mock <IQuizFlowManager>();

            mockManager.Setup(manager => manager.GetNextQuestion(It.IsAny <Quiz>()))
            .Returns(commandStub);
            var controller = new QuizFlowController(mockManager.Object, mockUow.Object);

            // Act
            var response = controller.GetNextQuestion(1);

            // Assert
            mockManager.Verify(mock => mock.GetNextQuestion(quiz), Times.Once);
            var objectResult = Assert.IsType <OkObjectResult>(response);
            var command      = Assert.IsType <QuizFlowCommandFinishContract>(objectResult.Value);
        }
コード例 #2
0
        public void GetNextQuestion_WhenQuizNotFound_ThrowsEntityNotFound()
        {
            // Arrange
            var mockQuizRepo = new Mock <IQuizRepository>();

            mockQuizRepo.Setup(repo => repo.GetByID(It.IsAny <int>()))
            .Returns(value: null);

            var mockUow = new Mock <IUnitOfWork>();

            mockUow.Setup(uow => uow.QuizRepository)
            .Returns(mockQuizRepo.Object);

            var mockManager = new Mock <IQuizFlowManager>();

            var controller = new QuizFlowController(mockManager.Object, mockUow.Object);

            // Act & Assert
            var ex = Assert.Throws <EntityNotFoundException>(() => controller.GetNextQuestion(1));

            Assert.Equal("Quiz", ex.EntityType);
            Assert.Equal(1, ex.EntityId);
        }