public void AddQuestionRedirectsToIndexIfSuccessful()
        {
            var repoSpy       = new QuestionRepoDouble();
            var questionDummy = new QuestionInput();
            var sut           = new HomeController(repoSpy);

            sut.WithCallTo(c => c.AddQuestion(questionDummy))
            .ShouldRedirectTo(c => c.Index());
        }
Exemple #2
0
 public ActionResult AddQuestion(QuestionInput question)
 {
     if (!ModelState.IsValid)
     {
         return(View(question));
     }
     repository.Add(question.ToQuestion());
     return(RedirectToAction("Index"));
 }
        public void AddQuestionInteractsWithQuestionRepo()
        {
            var repoDummy     = new QuestionRepoDouble();
            var questionDummy = new QuestionInput();
            var sut           = new HomeController(repoDummy);

            sut.WithCallTo(c => c.AddQuestion(questionDummy))
            .ShouldRedirectTo(c => c.Index());
            Assert.Equal(1, repoDummy.AddCallCount);
        }
        public void AddInvalidQuestionReturnsError()
        {
            var repoDummy     = new QuestionRepoDouble();
            var questionDummy = new QuestionInput();
            var sut           = new HomeController(repoDummy);

            sut.ViewData.ModelState.AddModelError("Title", "");
            sut.ViewData.ModelState.AddModelError("Body", "");
            sut.ViewData.ModelState.AddModelError("Tags", "");
            sut.WithCallTo(c => c.AddQuestion(questionDummy))
            .ShouldRenderDefaultView()
            .WithModel <QuestionInput>()
            .AndModelErrorFor(m => m.Title)
            .AndModelErrorFor(m => m.Body)
            .AndModelErrorFor(m => m.Tags);
        }
        public void ToQuestionReturnsCorrectResult(
            string tags,
            IEnumerable <string> expected)
        {
            var sut = new QuestionInput();

            sut.Tags  = tags;
            sut.Title = "foo";
            sut.Body  = "bar";

            var actual = sut.ToQuestion();

            Assert.Equal(expected, actual.Tags);
            Assert.Equal(sut.Title, actual.Title);
            Assert.Equal(sut.Body, actual.Body);
        }
Exemple #6
0
        private bool CanSave()
        {
            List <bool> cmpCanSave = new List <bool>()
            {
                QuestionInput.CanSave()
            };

            foreach (InputAnswerVM inputVM in Answers)
            {
                cmpCanSave.Add(inputVM.CanSave());
            }
            foreach (bool canSave in cmpCanSave)
            {
                if (!canSave)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #7
0
        public async Task <ServiceResult <QuestionModel> > AddQuestion(int id, QuestionInput input)
        {
            var note = await dbContext.Notes.FindAsync(id);

            if (note == null)
            {
                return(NotFound <QuestionModel>());
            }

            if (note.AuthorId != authService.GetUserId())
            {
                return(Unauthorized <QuestionModel>());
            }

            var question = mapper.Map <Question>(input);

            question.RootId = note.IsRoot ? id : note.RootId;

            note.Questions.Add(question);
            await dbContext.SaveChangesAsync();

            return(await questionService.Get(question.Id));
        }