如何构建新的 NUnit 约束
我有一个扩展方法,断言给定值是列表中的值之一。
public static void IsEither<T>(this T value, params T[] allowedValues)
{
EqualConstraint isInAllowed = null;
foreach (var allowed in allowedValues)
isInAllowed = isInAllowed == null ?
Is.EqualTo(allowed) : isInAllowed.Or.EqualTo(allowed);
Assert.That(value, isInAllowed);
}
我想知道是否有其他更好/优雅的方法来做到这一点,特别是使用 NUnit 的 ConstraintBuilder、ConstraintExpression、ConstraintOperator 等
I have an extension method that asserts a given value is one of the values in the list.
public static void IsEither<T>(this T value, params T[] allowedValues)
{
EqualConstraint isInAllowed = null;
foreach (var allowed in allowedValues)
isInAllowed = isInAllowed == null ?
Is.EqualTo(allowed) : isInAllowed.Or.EqualTo(allowed);
Assert.That(value, isInAllowed);
}
I wonder is there any other better/elegant way of doing this, particularly using NUnit's ConstraintBuilder, ConstraintExpression, ConstraintOperator etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NUnit 中有一个 CollectionAssert 应该会有所帮助。如果您断言一个项目集合包含另一个项目,您可以尝试如下操作:
There is a CollectionAssert in NUnit that should help. If you're asserting that a collection of items contains another item, you might try something like this: