简体   繁体   中英

How to build a new NUnit constraint

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM