【发布时间】:2012-07-30 09:33:52
【问题描述】:
我们编写了一个如下所示的测试。此测试要求我们为 CodeTableItem-class 创建了 en Equal-overload:
ICollection<CodeTableItem> expectedValutaList = new List<CodeTableItem>();
expectedValutaList.Add(new CodeTableItem("DKK", "DKK"));
expectedValutaList.Add(new CodeTableItem("EUR", "EUR"));
RepoDac target = new RepoDac();
var actual = target.GetValutaKd();
CollectionAssert.AreEqual(expectedValutaList.ToList(),actual.ToList());
测试工作正常,但是不幸地依赖于Equality-function,这意味着如果我用一个更多字段扩展CodeTableItem-class,并且忘记扩展Equals-function,单元测试仍然运行绿色,虽然我们没有测试所有领域。我们希望避免这种Equality 污染(参见Test Specific Equality),它只是为了符合测试而编写的。
我们尝试过使用OfLikeness,并以这种方式重写了测试:
ICollection<CodeTableItem> expectedValutaList = new List<CodeTableItem>();
expectedValutaList.Add(new CodeTableItem("DKK", "DKK"));
expectedValutaList.Add(new CodeTableItem("EUR", "EUR"));
var expectedValutaListWithLikeness =
expectedValutaList.AsSource().OfLikeness<List<CodeTableItem>>();
RepoDac target = new RepoDac();
ICollection<CodeTableItem> actual;
actual = target.GetValutaKd();
expectedValutaListWithLikeness.ShouldEqual(actual.ToList());
但测试失败,因为Capacity 不相等。我编写了多次通过反射运行的代码,并且通常最终实现了忽略字段的重载。有没有办法用OfLikeness 或ShouldEqual 忽略某些字段?或者有没有其他方法可以解决这个问题?
【问题讨论】:
标签: .net unit-testing collections autofixture semantic-comparison