public void Delete_Method_Should_Delete_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext();

            // Act
            var result = _controller.Delete(1) as RedirectToRouteResult;

            // Assert
            result.RouteValues["controller"].ShouldEqual("Home");
            result.RouteValues["action"].ShouldEqual("Index");

            _dbContext.Posts.Count().ShouldEqual(0);
        }
        public void Delete_Method_Should_Delete_Post_Via_Ajax()
        {
            // Arrange
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext(isAjaxRequest:true);

            // Act
            var result = _controller.Delete(1) as JsonResult;

            // Assert
            var serializer = new JavaScriptSerializer();
            var output = serializer.Serialize(result.Data);
            Assert.AreEqual(@"{""success"":true}", output);

            _dbContext.Posts.Count().ShouldEqual(0);
        }
        public void Detail_Method_Should_Return_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController();
            _controller.SetFakeControllerContext(true);

            // Act
            var result = _controller.Detail(1) as ViewResult;

            // Assert
            result.ViewName.ShouldEqual("Detail");
            var model = result.Model as PostDetailViewModel;
            model.ShouldNotBeNull();
            model.Title.Contains(FunctionalTestHelper.PostTitle);
            model.Content.Contains(FunctionalTestHelper.PostContent);
        }
        public void Edit_Method_Should_Save_Post()
        {
            // Arrange
            FunctionalTestHelper.CreateAutomapperMap();
            FunctionalTestHelper.CreateAccountAndBlog(_dbContext);
            FunctionalTestHelper.CreateOnePost(_dbContext);

            _controller = new PostController(new TagService());
            _controller.SetFakeControllerContext();

            // valueprovider is needed for TryUpdateModel() method
            FormCollection form = new FormCollection();
            _controller.ValueProvider = form.ToValueProvider();

            var input = new PostInput
            {
                Content = "edited content",
                Id = 1,
                Title = "edited title",
            };

            // Act
            var result = _controller.Edit(input) as RedirectToRouteResult;

            // Assert
            result.RouteValues["controller"].ShouldEqual("Post");
            result.RouteValues["action"].ShouldEqual("Detail");
            var post = _dbContext.Posts.SingleOrDefault(p => p.Id == 1);
            post.ShouldNotBeNull();
            post.Title.Contains("edited");
            post.Content.Contains("edited");
        }