NUnit:使用SemanticComparison .NET库

时间:2016-11-07 16:28:46

标签: .net unit-testing nunit autofixture semantic-comparison

我在我的单元测试(NUnit)中使用SemanticComparison并经常遇到这样的错误:

Expected Likeness of T But was T

我之前跟踪过这些错误,因为这是因为某些子属性不等于我在单元测试中定义的预期值。但错误消息只是具有这种非常一般的形式。而且我也没有在堆栈跟踪中看到任何内容。但我想这个库包含了一些自动显示不同的属性。手动跟踪它们都需要我很长时间。

为了完整起见,我的代码如下所示。但是我希望熟悉SemanticComparison的人能够阅读这个问题(它是一个库/ NuGet软件包,用于帮助Mark Seemann和Ploeh进行单元测试。它是AutoFixture的一个副项目。)。

_sut = new someService(apiKey);
var expectedResultaat = new someService {
    Prop1 = 1,
    Prop2 = 2,
}

var expected = expectedResultaat.AsSource().OfLikeness<ServiceResult>()
    .Without(i => i.Prop1)

 // Assert.
 Assert.AreEqual(expected, actual);

1 个答案:

答案 0 :(得分:2)

使用

expected.ShouldEqual(actual);

而不是

Assert.AreEqual(expected, actual);