public void MapTakeSurveyVMToSurveyFailsIfSurveyNotFound() { // Testing for a NullReferenceException if the survey cannot // be found. This would bubble up to be handled by the // application error handler. // Assert // 1 Mock the SurveyRepository class var mockSurveyRepository = new Mock<ISurveyRepository>(); mockSurveyRepository.Setup(r => r.GetSurvey(0)).Returns(mockData.GetSurvey3()); // 2 Mock the RespondentFactory class var mockRespondentFactory = new Mock<RespondentFactory>(); mockRespondentFactory.Setup(r => r.Create()).Returns(mockData.CreateRespondent()); // 3 Mock the ActualResponseFactory class var mockActualResponseFactory = new Mock<ActualResponseFactory>(); mockActualResponseFactory.Setup(f => f.Create()).Returns(mockData.CreateResponse()); // 4 set up the TakeSurveyViewModel class var inputViewModel = mockData.SetTakeSurveyViewModel_3(); // Instantiate the class being tested var mapper = new THSurveys.Mappings.MapTakeSurveyViewModelToSurvey(mockSurveyRepository.Object, mockRespondentFactory.Object, mockActualResponseFactory.Object); // Act // Execute the Map method try { var mappedSurvey = mapper.Map(inputViewModel); // Assert failure Assert.Fail("No exception thrown, expected a NullReferenece exception"); } catch (Exception e) { // Assert pass, the exception has been thrown. Assert.IsNotNull(e, "Exception expected"); Assert.IsInstanceOfType(e, typeof(NullReferenceException), "Expected NullReferenceException"); } }
public void MapTakeSurveyVMToSurveyOK() { // Assert // 1 Mock the SurveyRepository class var mockSurveyRepository = new Mock<ISurveyRepository>(); mockSurveyRepository.Setup(r => r.GetSurvey(3)).Returns(mockData.GetSurvey3()); // 2 Mock the RespondentFactory class var mockRespondentFactory = new Mock<RespondentFactory>(); mockRespondentFactory.Setup(r => r.Create()).Returns(mockData.CreateRespondent()); // 3 Mock the ActualResponseFactory class var mockActualResponseFactory = new Mock<ActualResponseFactory>(); mockActualResponseFactory.Setup(f => f.Create()).Returns(mockData.CreateResponse()); // 4 set up the TakeSurveyViewModel class var inputViewModel = mockData.SetTakeSurveyViewModel_3(); // Instantiate the class being tested var mapper = new THSurveys.Mappings.MapTakeSurveyViewModelToSurvey(mockSurveyRepository.Object, mockRespondentFactory.Object, mockActualResponseFactory.Object); // Act // Execute the Map method var mappedSurvey = mapper.Map(inputViewModel); // Assert // 1 Check the Survey class is not null Assert.IsNotNull(mappedSurvey, "Should have returned an instance of Survey class."); // 2 Check the Survey class has the correct survey Assert.AreEqual(3D, mappedSurvey.SurveyId, "Expected survey 3 to be returned"); // 3 Check the Survey class has the correct number of responses Assert.AreEqual(1, mappedSurvey.Respondents.Count(), "Expected 2 responses, as there are 2 questions."); // 4 Check the Survey class has the correct answer for the 1st question. var respondent = mappedSurvey.Respondents.First(); var q1Response = respondent.Responses.First(); Assert.AreEqual(5, q1Response.Response, "Expected answer 5 to question 1"); }