public async Task BlogController_Post_Returns_Model_If_Post_With_Id_Was_Found() { // Arrange BlogPost entity = new BlogPost() { Body = "Body", Preview = "Preview", PublishedAt = DateTime.UtcNow, Title = "Title", }; int id; // Use the shared database as this test only inserts // so other tests cannot interfer with its asserts. using (BlogContext context = new BlogContext(TestSetup.ConnectionString)) { context.Posts.Add(entity); await context.SaveChangesAsync(); id = entity.Id; } using (BlogController target = CreateTarget()) { // Act ActionResult result = await target.Post(id); // 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(string.Empty, view.ViewName); BlogPostViewModel model = view.Model as BlogPostViewModel; Assert.AreEqual(entity.Body, model.Body); Assert.AreEqual(id, model.Id); Assert.AreEqual(entity.Preview, model.Preview); Assert.IsTrue(entity.PublishedAt >= model.PublishedAt.AddMilliseconds(model.PublishedAt.Millisecond * -1)); Assert.AreEqual(entity.Title, model.Title); } }
public async Task BlogController_Post_Returns_HttpNotFound_Result_If_Post_With_Id_Not_Found() { // Arrange int id = int.MaxValue; // Use the shared database as the Id is made up so should not find anything. // If this is the first test to run, the shared SQL LocalDB instance will be created now. using (BlogController target = CreateTarget()) { // Act ActionResult result = await target.Post(id); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(HttpNotFoundResult)); } }