public async Task DeleteMessageAsync_MessageIsDeleted_WhenMessageIsFound() { using (var db = new PellokITHomeContext(Utilities.TestDbContextOptions())) { #region snippet1 // Arrange var seedMessages = PellokITHomeContext.GetSeedingArticles(); await db.AddRangeAsync(seedMessages); await db.SaveChangesAsync(); var recId = 1; var expectedMessages = seedMessages.Where(message => message.ID != recId).ToList(); #endregion #region snippet2 // Act await db.DeleteArticleAsync(recId); #endregion #region snippet3 // Assert var actualMessages = await db.Articles.AsNoTracking().ToListAsync(); Assert.Equal( expectedMessages.OrderBy(m => m.ID).Select(m => m.Title), actualMessages.OrderBy(m => m.ID).Select(m => m.Title)); #endregion } }
public async Task DeleteMessageAsync_NoMessageIsDeleted_WhenMessageIsNotFound() { using (var db = new PellokITHomeContext(Utilities.TestDbContextOptions())) { // Arrange var expectedMessages = PellokITHomeContext.GetSeedingArticles(); await db.AddRangeAsync(expectedMessages); await db.SaveChangesAsync(); var recId = 4; // Act try { await db.DeleteArticleAsync(recId); } catch { // recId doesn't exist } // Assert var actualMessages = await db.Articles.AsNoTracking().ToListAsync(); Assert.Equal( expectedMessages.OrderBy(m => m.ID).Select(m => m.Title), actualMessages.OrderBy(m => m.ID).Select(m => m.Title)); } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new PellokITHomeContext( serviceProvider.GetRequiredService < DbContextOptions <PellokITHomeContext> >())) { // Look for any movies. if (context.Articles.Any()) { return; // DB has been seeded } context.Articles.AddRange( new Article { Title = "Day01 Azure 的自我修煉", ReleaseDate = DateTime.Parse("2020-09-01"), Link = "https://ithelp.ithome.com.tw/articles/10233277", Count = 0, Category = "鐵人賽" }, new Article { Title = "Day02 申請Azure帳號", ReleaseDate = DateTime.Parse("2020-09-02"), Link = "https://ithelp.ithome.com.tw/articles/10233285", Count = 0, Category = "鐵人賽" }, new Article { Title = "Day03 Resource Group 資源群組", ReleaseDate = DateTime.Parse("2020-09-03"), Link = "https://ithelp.ithome.com.tw/articles/10233371", Count = 0, Category = "鐵人賽" }, new Article { Title = "Day04 Dotnet Core 專案", ReleaseDate = DateTime.Parse("2020-09-04"), Link = "https://ithelp.ithome.com.tw/articles/10233562", Count = 0, Category = "鐵人賽" } ); context.SaveChanges(); } }
public async Task DeleteAllMessagesAsync_MessagesAreDeleted() { using (var db = new PellokITHomeContext(Utilities.TestDbContextOptions())) { // Arrange var seedMessages = PellokITHomeContext.GetSeedingArticles(); await db.AddRangeAsync(seedMessages); await db.SaveChangesAsync(); // Act await db.DeleteAllArticlesAsync(); // Assert Assert.Empty(await db.Articles.AsNoTracking().ToListAsync()); } }
public async Task AddMessageAsync_MessageIsAdded() { using (var db = new PellokITHomeContext(Utilities.TestDbContextOptions())) { // Arrange var recId = 10; var expectedMessage = new Article() { ID = recId, Title = "Message" }; // Act await db.AddArticleAsync(expectedMessage); // Assert var actualMessage = await db.FindAsync <Article>(recId); Assert.Equal(expectedMessage, actualMessage); } }
public async Task GetMessagesAsync_MessagesAreReturned() { using (var db = new PellokITHomeContext(Utilities.TestDbContextOptions())) { // Arrange var expectedMessages = PellokITHomeContext.GetSeedingArticles(); await db.AddRangeAsync(expectedMessages); await db.SaveChangesAsync(); // Act var result = await db.GetArticlesAsync(); // Assert var actualMessages = Assert.IsAssignableFrom <List <Article> >(result); Assert.Equal( expectedMessages.OrderBy(m => m.ID).Select(m => m.Title), actualMessages.OrderBy(m => m.ID).Select(m => m.Title)); } }