AnyOf Constraint
AnyOfConstraint tests that a value equals any one of a set of expected values.
Note
Pass expected values as individual parameters, not as an array. To test if a collection contains a specific value, use SomeItemsConstraint instead.
Usage
Is.AnyOf(params object[] expected)
Modifiers
.IgnoreCase
.IgnoreWhiteSpace // NUnit 4.2+
.Using(IComparer comparer)
.Using(IEqualityComparer comparer)
.Using<T>(IComparer<T> comparer)
.Using<T>(IEqualityComparer<T> comparer)
.Using<T>(Comparison<T> comparer)
.Using<T>(Func<T, T, bool> comparer)
.UsingPropertiesComparer() // NUnit 4.1+
Examples
[Test]
public void AnyOfConstraint_Examples()
{
// Value must equal one of the options
Assert.That(42, Is.AnyOf(0, -1, 42, 100));
Assert.That("red", Is.AnyOf("red", "green", "blue"));
// With string comparison modifiers
Assert.That("RED", Is.AnyOf("red", "green", "blue").IgnoreCase);
// Negative test
Assert.That("yellow", Is.Not.AnyOf("red", "green", "blue"));
}
Notes
- This is for testing a single value against multiple options.
- For testing if a collection contains a value, use
Has.Member()orDoes.Contain(). - All comparison modifiers apply to the equality check between the actual and each expected value.
See Also
- SomeItems Constraint - Test if collection contains a value
- Equal Constraint - Test equality with single value
- Or Constraint - Alternative way to express multiple options