public async Task Checking_Reply_Count_Returns_Number_Of_Replies() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "Search_Database").Options; using (var ctx = new ApplicationDbContext(options)) { ctx.Posts.Add(new Post { Id = 1234 }); ctx.SaveChanges(); } using (var ctx = new ApplicationDbContext(options)) { var postService = new PostService(ctx); var post = postService.GetById(1234); await postService.AddReply(new PostReply { Post = post, Content = "Here's a post reply" }); } using (var ctx = new ApplicationDbContext(options)) { var postSercive = new PostService(ctx); var replyCount = postSercive.GetReplyCount(1234); Assert.AreEqual(replyCount, 1); } }
public async Task AddReply_WritesToDatabase() { //Arrange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "AddReply_Dtabase") .Options; //Act // Run the test against one instance of the context using (var context = new ApplicationDbContext(options)) { var postService = new PostService(context); await postService.AddReply(new PostReply()); } // Assert // Use a separate instance of the context to verify correct data was saved to database using (var context = new ApplicationDbContext(options)) { Assert.Equal(expected: 1, actual: context.PostReplies.Count()); } }