我正在进行删除的单元测试:
[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
调试后出现:
提前致谢!
答案 0 :(得分:2)
Assert.Equal
使用对象的.Equal
来比较两个对象。由于List<>
是一种参考类型,因此您的比较将失败。
使用CollectionAssert
来比较两个集合。我建议AreEquivalent
//...code removed for brevity
CollectionAssert.AreEquivalent(expectedSuppliers, actualSuppliers);