public async Task Edit_action_result_method_should_return_ViewResult_with_correct_Model_type() { // Arrange var id = 1; var expectedQuiz = _testData.GetTestQuizzes().FirstOrDefault(q => q.QuizId == id); _quizRepository.Setup(repo => repo.GetById(id)).ReturnsAsync(expectedQuiz); var expectedQuestions = _testData.GetTestQuestions().Where(q => q.QuizId == id); _questionRepository.Setup(repo => repo.GetByQuizId(id)).ReturnsAsync(expectedQuestions); var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object); // Act var result = await controller.Edit(id); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <EditQuizViewModel>(viewResult.ViewData.Model); Assert.NotNull(result); Assert.Equal(expectedQuiz.QuizId, model.Quiz.QuizId); Assert.Equal(2, model.Questions.Count()); }
public async Task Edit_When_Id_Is_Null_Returns_NotFoundResult() { // Arrange // Act var result = await _quizzesController.Edit(null); // Assert Assert.IsInstanceOf <NotFoundResult>(result); }
public async Task Edit_action_result_method_should_return_BadRequest_when_quiz_id_is_null() { // Arrange var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object); // Act var result = await controller.Edit(null); // Assert var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result); var model = Assert.IsAssignableFrom <BadRequestObjectResult>(badRequestObjectResult); Assert.Equal(model.StatusCode, badRequestObjectResult.StatusCode); Assert.NotNull(result); }
public async Task Edit_post_action_result_should_return_ViewResult_with_correct_Model_type_when_modelState_not_valid() { // Arrange var quiz = new Quiz { QuizId = 3 }; // Arrange var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object); controller.ModelState.AddModelError("Title", "Title is required"); // Act var result = await controller.Edit(quiz.QuizId, quiz); // Assert Assert.IsType <ViewResult>(result); }
public async Task Edit_action_result_method_should_return_NotFound_when_no_quiz_found_for_id() { // Arrange var id = 0; var expectedQuiz = _testData.GetTestQuizzes().FirstOrDefault(q => q.QuizId == id); _quizRepository.Setup(repo => repo.GetById(id)).ReturnsAsync(expectedQuiz); var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object); // Act var result = await controller.Edit(id); // Assert var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result); var model = Assert.IsAssignableFrom <NotFoundObjectResult>(notFoundObjectResult); Assert.Equal(model.StatusCode, notFoundObjectResult.StatusCode); Assert.Equal(model.Value, notFoundObjectResult.Value); Assert.NotNull(result); }
public async Task Edit_post_action_result_should_redirect_to_action_when_modelState_valid() { var id = 1; var quiz = _testData.GetTestQuizzes().FirstOrDefault(q => q.QuizId == id); _quizRepository.Setup(repo => repo.GetById(id)).ReturnsAsync(quiz); var httpContext = new DefaultHttpContext(); var tempData = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>()); var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object) { TempData = tempData }; // Act var result = await controller.Edit(quiz.QuizId, quiz); // Assert Assert.IsType <RedirectToActionResult>(result); }
public async Task Edit_post_action_result_method_should_return_BadRequest_when_id_received_different_to_quizId() { // Arrange var id = 0; var quiz = new Quiz { QuizId = 1, Title = "Test" }; _quizRepository.Setup(repo => repo.GetById(id)).ReturnsAsync(quiz); var controller = new QuizzesController(_quizRepository.Object, _questionRepository.Object); // Act var result = await controller.Edit(id, quiz); // Assert var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result); var model = Assert.IsAssignableFrom <BadRequestObjectResult>(badRequestObjectResult); Assert.Equal(model.StatusCode, badRequestObjectResult.StatusCode); Assert.NotNull(result); }