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); }
public void CompleteQuiz_WhenValidArguments_ReturnsOkResult() { // Arrange 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.CompleteQuiz(It.IsAny <Quiz>())); var controller = new QuizFlowController(mockManager.Object, mockUow.Object); // Act var response = controller.CompleteQuiz(1); // Assert mockManager.Verify(mock => mock.CompleteQuiz(quiz), Times.Once); var objectResult = Assert.IsType <OkResult>(response); }
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); }
public void CompleteQuiz_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.CompleteQuiz(1)); Assert.Equal("Quiz", ex.EntityType); Assert.Equal(1, ex.EntityId); }
public void Start_WhenQuizCreated_ReturnsOkResultWithQuiz() { // Arrange var quizTemplateStub = new QuizTemplate() { Id = 1 }; var mockQuizTemplateRepo = new Mock <IQuizTemplateRepository>(); mockQuizTemplateRepo.Setup(repo => repo.GetByID(It.IsAny <int>())) .Returns(quizTemplateStub); var mockUow = new Mock <IUnitOfWork>(); mockUow.Setup(uow => uow.QuizTemplateRepository) .Returns(mockQuizTemplateRepo.Object); var quizStub = new Quiz() { Id = 1 }; var mockManager = new Mock <IQuizFlowManager>(); mockManager.Setup(manager => manager.StartNewQuiz(It.IsAny <QuizTemplate>())) .Returns(quizStub); var controller = new QuizFlowController(mockManager.Object, mockUow.Object); // Act var response = controller.Start(1); // Assert mockManager.Verify(mock => mock.StartNewQuiz(quizTemplateStub), Times.Once); var objectResult = Assert.IsType <OkObjectResult>(response); var quiz = Assert.IsType <Quiz>(objectResult.Value); Assert.Equal(1, quiz.Id); }
public void AnswerQuestion_WhenQuestionNotFound_ThrowsEntityNotFound() { // Arrange var quiz = new Quiz(); var mockQuizRepo = new Mock <IQuizRepository>(); mockQuizRepo.Setup(repo => repo.GetByID(It.IsAny <int>())) .Returns(quiz); 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("Question", ex.EntityType); Assert.Equal(2, ex.EntityId); }