public async Task BlogController_New_For_Post_Does_Not_Add_Post_If_Invalid()
        {
            // Arrange
            DateTime now = DateTime.UtcNow;
            now = now.AddMilliseconds(now.Millisecond * -1);

            NewPostViewModel model = new NewPostViewModel();

            using (BlogController target = new BlogController())
            {
                target.ModelState.AddModelError("Body", "A body must be specified.");

                // Act
                ActionResult result = await target.New(model);

                // Assert
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(ViewResult));

                ViewResult view = result as ViewResult;

                Assert.IsNotNull(view.Model);
                Assert.AreSame(model, view.Model);
                Assert.AreEqual(string.Empty, view.ViewName);
            }
        }
        public async Task<ActionResult> New(NewPostViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return View(model);
            }

            BlogPost entity = new BlogPost()
            {
                Body = model.Body,
                Preview = model.Preview,
                PublishedAt = DateTime.UtcNow,
                Title = model.Title,
            };

            using (var context = _contextFactory())
            {
                context.Posts.Add(entity);
                await context.SaveChangesAsync();
            }

            return await Latest();
        }
        public async Task BlogController_New_For_Post_Adds_Post_If_Valid()
        {
            // Arrange
            DateTime now = DateTime.UtcNow;
            now = now.AddMilliseconds(now.Millisecond * -1);

            NewPostViewModel model = new NewPostViewModel()
            {
                Title = "New Blog Post Title",
                Body = "This is my blog post about things.",
                Preview = "This is a preview.",
            };

            // Use the shared database as this test only inserts
            // so other tests cannot interfer with its asserts.
            using (BlogController target = CreateTarget())
            {
                // Act
                ActionResult result = await target.New(model);

                // Assert
                Assert.IsNotNull(result);
                Assert.IsInstanceOfType(result, typeof(ViewResult));

                ViewResult view = result as ViewResult;

                Assert.IsNotNull(view.Model);
                Assert.IsInstanceOfType(view.Model, typeof(BlogPostViewModel));
                Assert.AreEqual("Post", view.ViewName);

                BlogPostViewModel viewModel = view.Model as BlogPostViewModel;

                Assert.AreEqual(model.Body, viewModel.Body);
                Assert.AreNotEqual(0, viewModel.Id);
                Assert.AreEqual(model.Preview, viewModel.Preview);
                Assert.IsTrue(viewModel.PublishedAt >= now);
                Assert.AreEqual(model.Title, viewModel.Title);
            }
        }