我们编写了一个如下所示的测试.此测试要求我们Equal为CodeTableItem-class 创建了en -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());
Run Code Online (Sandbox Code Playgroud)
测试工作正常,但是对Equality函数有一个不幸的依赖,这意味着如果我CodeTableItem用一个字段扩展-class,并忘记扩展Equals-function,单元测试仍然会运行绿色,尽管我们不测试所有字段.我们希望避免这种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());
Run Code Online (Sandbox Code Playgroud)
但测试失败是因为Capacity不相等.我编写了多次运行反射的代码,并且通常最终实现了忽略字段的重载.有没有办法用OfLikeness或忽略某些字段ShouldEqual?或者还有其他方法可以解决这个问题吗?
.net collections unit-testing autofixture semantic-comparison
我试图了解如何使用使用简单类的两个实例的CreateProxy()功能Likeness<T>().
public class Band
{
public string Strings { get; set; }
public string Brass { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
通过下面的测试中,我使用Fixture到Create<T>一个Band实例与两个字符串属性的值.
[Fact]
public void Equality_Behaves_As_Expected()
{
// arrange
var fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization());
var original = fixture.Create<Band>();
// Brass something like --> "Brass65756b89-d9f3-42f8-88fc-ab6de5ae65cd"
// Strings something like --> "Strings7439fa1b-014d-4544-8428-baea66858940"
// act
var dupe = new Band {Brass = original.Brass,
Strings = original.Strings};
// Brass same as original's like …Run Code Online (Sandbox Code Playgroud) 是否可以在AutoFixture SemanticComparison 中为对象的指定属性或指定类型设置自定义比较器。
例如,您需要比较具有 DateTime 类型属性的对象。您想使用 Likenes 比较它们,但您希望比较的精度在毫秒级别丢弃差异(可能在从数据库检索 DateTime 后丢失了精度)。