Exemplo n.º 1
0
        public ActionResult Create(CreateInputModel inputModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    CreateViewModel viewModel = Mapper.Map(inputModel, new CreateViewModel(User.Identity)
                                                                           {
                                                                               Header = "Ask a Question"
                                                                           });
                    return View("Create", viewModel);
                }

                Core.Entities.Question question = Mapper.Map<CreateInputModel, Core.Entities.Question>(inputModel);
                question.CreatedByUserId = User.Identity.UserId;

                _questionService.Create(question);

                return RedirectToAction("Index", "Home", new { area = "" });
            }
            catch(Exception exception)
            {
                ModelState.AddModelError("RuRoh", exception.Message);
                CreateViewModel viewModel = Mapper.Map(inputModel, new CreateViewModel(User.Identity)
                                                                       {
                                                                           Header = "Ask a Question"
                                                                       });
                return View("Create", viewModel);
            }
        }
        public void GivenAnInValidQuestion_Create_ReturnsAResultView()
        {
            using (IDocumentSession documentSession = DocumentStore.OpenSession())
            {
                // Arrange.
                IQuestionService questionService = new QuestionService(documentSession);
                var questionsController = new QuestionsController(documentSession, questionService);
                ControllerUtilities.SetUpControllerContext(questionsController);
                var createInputModel = new CreateInputModel
                                           {
                                               Content = "Some content",
                                               Subject = null,
                                               // RuRoh - dat ist missin'
                                               Tags = "tag1 tag2 tag3-3-3"
                                           };

                // Now pretend the model binding raised an error with the input model.
                questionsController.ModelState.AddModelError("key", "error message");

                // Act.
                var result = questionsController.Create(createInputModel) as ViewResult;

                // Assert.
                Assert.NotNull(result);
                Assert.Equal("Create", result.ViewName);
            }
        }
        public void GivenAValidQuestionAndALoggedInUser_Create_AddsTheQuestionAndRedicects()
        {
            // Arrange.
            IQuestionService questionService = new QuestionService();
            var questionsController = new QuestionsController(DocumentStore, questionService);
            ControllerUtilities.SetUpControllerContext(questionsController, "users/1");
            var createInputModel = new CreateInputModel
                                   {
                                       Content = "Some content",
                                       Subject = "Subject",
                                       Tags = "tag1 tag2 tag3-3-3"
                                   };
            // Act.
            var result = questionsController.Create(createInputModel) as RedirectToRouteResult;

            // Assert.
            Assert.NotNull(result);
            Assert.Equal("Index", result.RouteValues["action"]);
        }
        public void GivenAValidQuestionAndNoOneIsLoggedIn_Create_ReturnsAResultView()
        {
            // Arrange.
            IQuestionService questionService = new QuestionService();
            var questionsController = new QuestionsController(DocumentStore, questionService);
            ControllerUtilities.SetUpControllerContext(questionsController);
            var createInputModel = new CreateInputModel
                                   {
                                       Content = "Some content",
                                       Subject = "Subject",
                                       Tags = "tag1 tag2 tag3-3-3"
                                   };
            // Act.
            var result = questionsController.Create(createInputModel) as ViewResult;

            // Assert.
            Assert.NotNull(result);
            Assert.Equal("Create", result.ViewName);
        }