Ejemplo n.º 1
0
        public async Task Create_post_action_result_should_redirect_to_action_when_modelState_valid()
        {
            // Arrange
            var answer = new Answer
            {
                QuestionId = 1,
                AnswerId   = 1,
                AnswerText = "Test"
            };

            var httpContext = new DefaultHttpContext();

            var tempData = new TempDataDictionary(httpContext, Mock.Of <ITempDataProvider>());

            var controller = new AnswersController(_answerRepository.Object)
            {
                TempData = tempData
            };

            // Act
            var result = await controller.Create(answer);

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
        }
Ejemplo n.º 2
0
        public void Create_action_should_return_BadRequest_when_questionId_is_not_valid()
        {
            // Arrange
            var controller = new AnswersController(_answerRepository.Object);

            // Act
            var result = controller.Create(id: null);

            // Assert
            var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result);
            var model = Assert.IsAssignableFrom <BadRequestObjectResult>(badRequestObjectResult);

            Assert.Equal(model.StatusCode, badRequestObjectResult.StatusCode);
            Assert.NotNull(result);
        }
Ejemplo n.º 3
0
        public void Create_action_should_return_Create_View_with_correct_model()
        {
            // Arrange
            var questionId = 1;

            var answersForQuestion = _testData.GetTestAnswers().Where(q => q.QuestionId == questionId);

            _answerRepository.Setup(repo => repo.GetByQuestionId(questionId)).ReturnsAsync(answersForQuestion.ToList);

            var controller = new AnswersController(_answerRepository.Object);

            // act
            var result = controller.Create(questionId);

            // assert
            Assert.IsType <ViewResult>(result);
            Assert.NotNull(result);
        }
Ejemplo n.º 4
0
        public async Task Create_post_action_result_should_return_ViewResult_with_correct_Model_type_when_modelState_not_valid()
        {
            // Arrange
            var answer = new Answer
            {
                AnswerId = 20,
            };

            // Arrange
            var controller = new AnswersController(_answerRepository.Object);

            controller.ModelState.AddModelError("Title", "Title is required");

            // Act
            var result = await controller.Create(answer);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.IsAssignableFrom <Answer>(viewResult.ViewData.Model);
        }
Ejemplo n.º 5
0
 public void Create_ThrowException()
 {
     Assert.That(() => controller.Create(null), Throws.Exception);
 }