我正在编写一些单元测试并想检查结果列表。
这是我正在做的一个简单例子:
[Test]
public void FilterSomething_Test()
{
List<MyClass> testdata = new List<MyClass>
{
new MyClass { SomeProperty = "expectedValue" },
new MyClass { SomeProperty = "expectedValue" },
new MyClass { SomeProperty = "unexpectedValue" },
new MyClass { SomeProperty = "unexpectedValue" },
new MyClass { SomeProperty = null },
}
List<MyClass> result = FilterSomething(testdata);
Assert.That(
result.Where(r => r.SomeProperty == "expectedValue"),
Has.Exactly(2).Items,
"Two Items should match this..");
}
失败测试的输出:
Two Items should match this..
Expected: exactly 2 items
But was: no items
输出没有解释出了什么问题。
说明:我有多个测试的测试数据。这就是为什么我要检查每个测试中的特定项目。
我的问题:
有没有一种方法可以检查列表中的项目计数并从 NUnit
获取正确的消息?
也许是这样的
Assert.That(result, Contains.Exacly(2).Items.Which(i => i.SomeProperty == "expectedValue"))
最佳答案
Matches
约束表达式专用于此。
这种情况下的用法可能如下所示:
Assert.That(result,
Has.Exactly(2).Matches<MyClass>(r => r.SomeProperty == "expectedValue"),
"Two Items should match this..");
关于c# - 如何将 "Assert.That()"列表中的项目与 NUnit 匹配某些条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57413661/