public async Task CreateActionResult_ReturnsNotFoundObjectResultForIncorrectEntity() { // Arrange var nonExistentSurveyId = 999; string surveyName = "Survey 999"; var mockRepo = new Mock <CompassDBContext>(); mockRepo.SetupAllProperties(); //mockRepo.Setup(repo => repo.GetSurveyAsync(nonExistentSurveyId)) // .ReturnsAsync(sample_survey); var controller = new SurveysController(mockRepo.Object); var survey = new Survey() { Name = surveyName, Id = nonExistentSurveyId, Questions = { } }; // Act var result = await controller.PostSurvey(survey); // Assert var actionResult = Assert.IsType <ActionResult <Questions> >(result); Assert.IsType <NotFoundObjectResult>(actionResult.Result); }
public async Task Verify_DummySurveyCreated() { // Arrange string surveyName = "Survey 4"; int surveyId = 4; Survey sample_survey = GetSurvey(); var mockRepo = new Mock <CompassDBContext>(); mockRepo.SetupAllProperties(); mockRepo.Setup(repo => repo.GetSurveyAsync(surveyId)) .ReturnsAsync(sample_survey); var controller = new SurveysController(mockRepo.Object); // Act var result = await controller.PostSurvey(sample_survey); // Assert var okResult = Assert.IsType <CreatedAtActionResult>(result); var returnSession = Assert.IsType <Survey>(okResult.Value); Assert.Equal(surveyId, returnSession.Id); Assert.Equal(surveyName, returnSession.Name); Assert.Equal(1, returnSession.Questions.Count); }
public async Task Verify_AddAndUpdateDummySurvey() { // Arrange string surveyName = "Survey 4"; int surveyId = 4; Survey sample_survey = GetSurvey(); Survey updated_survey = sample_survey; updated_survey.Name = "Updated survey 4"; var mockRepo = new Mock <CompassDBContext>(); mockRepo.SetupAllProperties(); mockRepo.Setup(repo => repo.GetSurveyAsync(surveyId)) .ReturnsAsync(sample_survey); mockRepo.Setup(repo => repo.UpdateSurveyAsync(surveyId, sample_survey)) .Returns(Task.CompletedTask).Verifiable(); var controller = new SurveysController(mockRepo.Object); // Act var result = await controller.PostSurvey(sample_survey); // Assert var okResult = Assert.IsType <CreatedAtActionResult>(result); var returnSession = Assert.IsType <Survey>(okResult.Value); //Act sample_survey.Name = "Updated survey 4"; var updateResult = await controller.PutSurvey(surveyId, sample_survey); var okUpdateResult = Assert.IsType <OkObjectResult>(updateResult); var updatedSurveyObject = Assert.IsType <Survey>(okUpdateResult.Value); Assert.Equal(updated_survey.Name, updatedSurveyObject.Name); }
public async Task Verify_IncorrectSurveyCreation() { var mockDBContext = new Mock <CompassDBContext>(); var surveyController = new SurveysController(mockDBContext.Object); surveyController.ModelState.AddModelError("error", "Incorrect data"); // Act var result = await surveyController.PostSurvey(null); // Assert Assert.IsType <BadRequestObjectResult>(result); }