方法的子集作为 NUnit 中的实际参数

SubsetOf method as actual paramter in NUnit

我正在用 NUnit 编写单元测试。我正在测试 returns 元素集合的方法。我想检查它是否包含另一个集合的子集。所以,我创建了一个测试,它看起来是:

// Ignore other lines for simplicity
Assert.That(new[] { 1, 3, 4, 6 }, Is.SubsetOf(actual));

但是,当我的测试失败时,出现以下错误:

Expected: subset of < 1, 2, 3, 4, 5 >
But was:  < 1, 3, 4, 6 >

这不太正确。它应该是这样的:

Expected: < 1, 3, 4, 6 >
But was: < 1, 2, 3, 4, 5 >

或类似的东西。

主要思想是我需要更改 expectedactual 参数但不能。 例如。我不能这样写:

Assert.That(Is.SubsetOf(actual), new []{1, 3, 4, 6});

因为没有这个方法

我也知道我可以 运行 循环或其他一些手动解决方案,但我想知道是否有 NUnit 就绪的解决方案。

您正在寻找超集约束,而不是子集约束:

Assert.That(actual, Is.SupersetOf(new[] { 1, 3, 4, 6 }));

https://docs.nunit.org/articles/nunit/writing-tests/constraints/CollectionSupersetConstraint.html