Exemplo n.º 1
0
        public void AnswerQuestion_WhenValidArguments_ReturnsOkResult()
        {
            // Arrange
            var question = new Question()
            {
                Id = 2
            };
            var quiz = new Quiz();

            quiz.Questions.Add(question);

            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.AnswerQuestion(It.IsAny <Question>(), It.IsAny <int>()));
            var controller = new QuizFlowController(mockManager.Object, mockUow.Object);

            // Act
            var response = controller.AnswerQuestion(1, 2, 3);

            // Assert
            mockManager.Verify(mock => mock.AnswerQuestion(question, 3), Times.Once);
            var objectResult = Assert.IsType <OkResult>(response);
        }
Exemplo n.º 2
0
        public void AnswerQuestion_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.AnswerQuestion(1, 2, 3));

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