1

我正在对删除进行单元测试:

[Fact]
 public async Task DeleteSupplierAsync_SupplierIsDeleted_WhenSupplierIsFound()
 {
     using (var db = new SulferFireDbContext(Utilities.TestingDbContextOptions()))
     {
         // Arrange
         var seedSuppliers = SulferFireDbContext.GetSeedingSuppliers();
         await db.AddRangeAsync(seedSuppliers);
         await db.SaveChangesAsync();
         var recId = 123;
         var expectedSuppliers = seedSuppliers.Where(supplier => supplier.ID != recId).ToList();
         // Act
         await db.DeleteSupplierAsync(recId);
         // Assert
         var actualSuppliers = await db.Suppliers.AsNoTracking().ToListAsync();
         Assert.Equal(expectedSuppliers, actualSuppliers);
     }
}

问题是,expectedSuppliers似乎actualSuppliers是平等的,但测试失败了。这出现在Assert.Equals我调试之后:

例外

4

1 回答 1

2

Assert.Equal使用对象的.Equal比较两个对象。作为List<>参考类型,此比较将失败,因为您已经体验过。

CollectionAssert改为用于比较两个集合。我会建议AreEquivalent

//...code removed for brevity

CollectionAssert.AreEquivalent(expectedSuppliers, actualSuppliers);
于 2018-03-17T01:25:58.547 回答