开发者

How to build a new NUnit constraint

开发者 https://www.devze.com 2023-02-20 02:13 出处:网络
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)

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

精彩评论

暂无评论...
验证码 换一张
取 消