Beispiel #1
0
        public void AddCommentSavesCommentToPostRepositoryAndRedirects()
        {
            var postUrl = new PostUrlViewModel { Year = 2013, Month = 4, UrlTitle = "a-test" };
            var returnedPost = new Post();

            Mock.Get(repository).Setup(r => r.SinglePost(postUrl.Url)).Returns(returnedPost);

            var model = new PostViewModel
            {
                CommenterName = "George",
                CommenterEmail = "*****@*****.**",
                CommenterWebsite = "vandelay.com",
                Comment = "A comment"
            };

            var result = controller.AddComment(postUrl, model) as RedirectToRouteResult;

            Assert.That(returnedPost.Comments,
                Has.Exactly(1).Matches<Comment>(c => c.Text == model.Comment
                                                     && c.Name == model.CommenterName
                                                     && c.Email == model.CommenterEmail
                                                     && c.Website == model.CommenterWebsite));
            Assert.NotNull(result);
            Assert.That(result.RouteValues["action"], Is.EqualTo("Single"));
            Mock.Get(repository).Verify(r => r.Save(returnedPost));
        }
Beispiel #2
0
        public ActionResult AddComment(PostUrlViewModel postUrl, PostViewModel model)
        {
            if (!ModelState.IsValid) return View(model);

            var post = repository.SinglePost(postUrl.Url);

            post.Comments.Add(new Comment
            {
                Text = model.Comment,
                Email = model.CommenterEmail,
                Name = model.CommenterName,
                Website = model.CommenterWebsite
            });

            repository.Save(post);

            return RedirectToAction("Single");
        }
Beispiel #3
0
        public void GetReturnsCorrectPostFromRepository()
        {
            var postUrl = new PostUrlViewModel { Year = 2013, Month = 4, UrlTitle = "a-test" };
            var post = new Post();
            var expectedModel = new PostViewModel();

            Mock.Get(repository)
                .Setup(r => r.SinglePost(It.IsAny<string>()))
                .Returns(post);

            Mock.Get(mapper)
                .Setup(m => m.Map<PostViewModel>(post))
                .Returns(expectedModel);

            var result = (ViewResult)controller.Single(postUrl);
            var model = (PostViewModel)result.Model;

            Mock.Get(repository).Verify(r => r.SinglePost(postUrl.Url));
            Assert.That(model, Is.EqualTo(expectedModel));
        }
Beispiel #4
0
        public void CreateReturnsInvalidPostWithModelErrors()
        {
            var postUrl = new PostUrlViewModel { Year = 2013, Month = 4, UrlTitle = "a-test" };
            var incompleteComment = new PostViewModel();

            controller.ModelState.AddModelError("", "mock error message");

            var response = (ViewResult)controller.AddComment(postUrl, incompleteComment);

            Assert.That(response.ViewData.ModelState.IsValid, Is.False);
        }