public void Should_Generate_Random_Quiz_Questions() { // Arrange var randomCounter = new Dictionary <string, int>(); var mockQuizQuestionRepository = new Mock <IQuizQuestionRepository>(); mockQuizQuestionRepository.Setup(m => m.GetAll()).Returns(GetMockQuizQuestions()); var service = new QuizQuestionService(mockQuizQuestionRepository.Object, new Mock <IValidationDictionary>().Object); // Act for (var i = 1; i < 500; i++) { var question = service.GetRandomQuizQuestion(); if (randomCounter.ContainsKey(question.Question)) { randomCounter[question.Question]++; } else { randomCounter.Add(question.Question, 1); } } // Assert // NOTE: In this instance since it's not easy to truly test for random, I'm outputting the // result for a quick check by eye. foreach (var key in randomCounter.Keys) { Console.WriteLine($"{key} was retrieved {randomCounter[key]} times"); } }
public void Should_Get_Random_Quiz_Question_From_Repository() { // Arrange var mockQuizQuestionRepository = new Mock <IQuizQuestionRepository>(); mockQuizQuestionRepository.Setup(m => m.GetAll()).Returns(GetMockQuizQuestions()); var service = new QuizQuestionService(mockQuizQuestionRepository.Object, new Mock <IValidationDictionary>().Object); // Act service.GetRandomQuizQuestion(); // Assert mockQuizQuestionRepository.Verify(m => m.GetAll(), Times.Once); }
public void Should_Not_Error_On_Generating_Random_Questions_If_No_Questions_Configured() { // Arrange var randomCounter = new Dictionary <string, int>(); var mockQuizQuestionRepository = new Mock <IQuizQuestionRepository>(); mockQuizQuestionRepository.Setup(m => m.GetAll()).Returns(new List <QuizQuestion>()); var service = new QuizQuestionService(mockQuizQuestionRepository.Object, new Mock <IValidationDictionary>().Object); // Act var question = service.GetRandomQuizQuestion(); // Assert mockQuizQuestionRepository.Verify(m => m.GetAll(), Times.Once); Assert.IsNull(question); }