I have an extension method that asserts a given value is one of the values in the list.
public static void开发者_Go百科 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
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:
public static void IsEither<T>(this T value, params T[] allowedValues)
{
CollectionAssert.Contains(allowedValues, value);
}
精彩评论