Search Results for

    Show / Hide Table of Contents

    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

    1. This is for testing a single value against multiple options.
    2. For testing if a collection contains a value, use Has.Member() or Does.Contain().
    3. 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
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0