Exemplo n.º 1
0
        public void Should_Not_Save_Invalid_QuizQuestions()
        {
            // Arrange
            var mockQuizQuestionRepository = new Mock <IQuizQuestionRepository>();
            var mockValidationDictionary   = new MockValidationDictionary();
            var service = new QuizQuestionService(mockQuizQuestionRepository.Object, mockValidationDictionary);

            // Act
            bool result = service.AddNewQuizQuestion(new QuizQuestionViewModel {
                Question = "Q", Answer = ""
            });

            // Assert
            Assert.IsFalse(mockValidationDictionary.IsValid);
            Assert.IsTrue(mockValidationDictionary.Errors.ContainsKey("Answer"));
            mockQuizQuestionRepository.Verify(m => m.AddQuestion(It.IsAny <QuizQuestion>()), Times.Never);
            mockQuizQuestionRepository.Verify(m => m.SaveChanges(), Times.Never);
        }
Exemplo n.º 2
0
        public void Should_Call_Save_Methods_When_Question_Saved()
        {
            // Arrange
            var mockQuizQuestionRepository = new Mock <IQuizQuestionRepository>();
            var mockValidationDictionary   = new MockValidationDictionary();
            var service = new QuizQuestionService(mockQuizQuestionRepository.Object, mockValidationDictionary);

            // Act
            var result = service.AddNewQuizQuestion(new QuizQuestionViewModel {
                Question = "Q", Answer = "A"
            });

            // Assert
            Assert.IsTrue(mockValidationDictionary.IsValid);                                              // Should be perfectly valid and therefore ...
            mockQuizQuestionRepository.Verify(m => m.AddQuestion(It.IsAny <QuizQuestion>()), Times.Once); // ... changes are made to the table ...
            mockQuizQuestionRepository.Verify(m => m.SaveChanges(), Times.Once);                          // ... and saved ...
            Assert.IsTrue(result);                                                                        // ... returning true
        }