public void BlogInitializer_Adds_And_Persists_Data_To_Database() { // Arrange // Use a new database so that our results are not interfered with by other tests. // If this is the first test to run, the shared SQL LocalDB instance will be created now. string connectionString = TestSetup.GetConnectionStringForNewDatabase(); BlogInitializer target = new BlogInitializer(); using (BlogContext context = new BlogContext(connectionString)) { // Act target.InitializeDatabase(context); } int actual; using (BlogContext context = new BlogContext(connectionString)) { // Assert actual = context.Posts.Count(); } Assert.AreEqual(2, actual); }
public async Task BlogController_Archive_Returns_All_Posts_If_There_Are_Less_Than_Five_Posts_In_The_Database() { // Arrange // Use a new database so that our results are not interfered with by other tests. // If this is the first test to run, the shared SQL LocalDB instance will be created now. string connectionString = TestSetup.GetConnectionStringForNewDatabase(); DateTime now = DateTime.UtcNow; using (BlogContext context = new BlogContext(connectionString)) { context.Posts.Add(new BlogPost() { Body = "Body 1", Preview = "Preview 1", Title = "Title 1", PublishedAt = now }); context.Posts.Add(new BlogPost() { Body = "Body 2", Preview = "Preview 2", Title = "Title 2", PublishedAt = now.AddDays(1) }); await context.SaveChangesAsync(); } using (BlogController target = CreateTarget(connectionString)) { // Act ActionResult result = await target.Archive(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult view = result as ViewResult; Assert.IsInstanceOfType(view.Model, typeof(ICollection<BlogPostViewModel>)); Assert.AreEqual(string.Empty, view.ViewName); ICollection<BlogPostViewModel> model = view.Model as ICollection<BlogPostViewModel>; Assert.AreEqual(2, model.Count); Assert.AreEqual("Title 2", model.First().Title); Assert.AreEqual("Title 1", model.Last().Title); } }
public async Task BlogController_Archive_Returns_All_Posts_If_There_Are_More_Than_Five_Posts_In_The_Database() { // Arrange // We can use the shared database as the test data will artificially put items far // enough in the future that we will still get the results we want and because our // expected result set has a maximum size it does not matter if there is data already present. // If this is the first test to run, the shared SQL LocalDB instance will be created now. DateTime now = DateTime.UtcNow.AddYears(3); using (BlogContext context = new BlogContext(TestSetup.ConnectionString)) { context.Posts.Add(new BlogPost() { Body = "Body 1", Preview = "Preview 1", Title = "Future Title 1", PublishedAt = now }); context.Posts.Add(new BlogPost() { Body = "Body 2", Preview = "Preview 2", Title = "Future Title 2", PublishedAt = now.AddDays(1) }); context.Posts.Add(new BlogPost() { Body = "Body 3", Preview = "Preview 3", Title = "Future Title 3", PublishedAt = now.AddDays(2) }); context.Posts.Add(new BlogPost() { Body = "Body 4", Preview = "Preview 4", Title = "Future Title 4", PublishedAt = now.AddDays(3) }); context.Posts.Add(new BlogPost() { Body = "Body 5", Preview = "Preview 5", Title = "Future Title 5", PublishedAt = now.AddDays(4) }); context.Posts.Add(new BlogPost() { Body = "Body 6", Preview = "Preview 6", Title = "Future Title 6", PublishedAt = now.AddDays(5) }); context.Posts.Add(new BlogPost() { Body = "Body 7", Preview = "Preview 7", Title = "Future Title 7", PublishedAt = now.AddDays(6) }); await context.SaveChangesAsync(); } using (BlogController target = CreateTarget()) { // Act ActionResult result = await target.Archive(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult view = result as ViewResult; Assert.IsInstanceOfType(view.Model, typeof(ICollection<BlogPostViewModel>)); Assert.AreEqual(string.Empty, view.ViewName); ICollection<BlogPostViewModel> model = view.Model as ICollection<BlogPostViewModel>; Assert.AreEqual(5, model.Count); Assert.AreEqual("Future Title 7", model.First().Title); Assert.AreEqual("Future Title 3", model.Last().Title); } }
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_Index_Model_Is_For_Latest_Post_If_There_Are_Posts_In_The_Database() { // Arrange // Use a new database so that our results are not interfered with by other tests. // If this is the first test to run, the shared SQL LocalDB instance will be created now. string connectionString = TestSetup.GetConnectionStringForNewDatabase(); using (BlogContext context = new BlogContext(connectionString)) { context.Posts.Add(new BlogPost() { Title = "Title 1", Body = "Post 1", Preview = "Preview 1", PublishedAt = new DateTime(2015, 6, 10, 12, 00, 00, DateTimeKind.Utc) }); context.Posts.Add(new BlogPost() { Title = "Title 2", Body = "Post 2", Preview = "Preview 2", PublishedAt = new DateTime(2015, 6, 11, 12, 00, 00, DateTimeKind.Utc) }); context.Posts.Add(new BlogPost() { Title = "Title 3", Body = "Post 3", Preview = "Preview 3", PublishedAt = new DateTime(2015, 6, 11, 13, 00, 00, DateTimeKind.Utc) }); await context.SaveChangesAsync(); } using (BlogController target = CreateTarget(connectionString)) { // Act ActionResult result = await target.Index(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult view = result as ViewResult; Assert.IsNotNull(view.Model); Assert.IsInstanceOfType(view.Model, typeof(HomePageViewModel)); Assert.AreEqual(string.Empty, view.ViewName); HomePageViewModel model = view.Model as HomePageViewModel; Assert.AreEqual("Preview 3", model.LatestPostPreview); Assert.AreEqual("Title 3", model.LatestPostTitle); } }