Пример #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));
        }
Пример #2
0
        public ActionResult Single(PostUrlViewModel postUrl)
        {
            var entity = repository.SinglePost(postUrl.Url);

            if (entity == null) return HttpNotFound();

            var model = mapper.Map<PostViewModel>(entity);

            return View(model);
        }
Пример #3
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");
        }
Пример #4
0
        public void NonExistentPostUrlReturns404()
        {
            var badUrl = new PostUrlViewModel { Year = 2012, Month = 3, UrlTitle = "doesnt-exist" };

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

            var result = controller.Single(badUrl);

            Mock.Get(repository).Verify(r => r.SinglePost(badUrl.Url));
            Assert.That(result, Is.InstanceOf<HttpNotFoundResult>());
        }
Пример #5
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));
        }
Пример #6
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);
        }