Search Results for

    Show / Hide Table of Contents

    Attribute Constraint

    AttributeConstraint tests for the existence of an attribute on a type and then applies a further constraint to that attribute's properties.

    Usage

    Has.Attribute(Type attributeType).<constraint>
    Has.Attribute<TAttribute>().<constraint>
    

    Examples

    [TestFixture(Description = "Integration tests")]
    [Category("Database")]
    public class AttributeConstraintExamplesFixture
    {
        [Test]
        public void AttributeConstraint_Examples()
        {
            // Test that a type has an attribute with specific properties
            Assert.That(typeof(AttributeConstraintExamplesFixture), Has.Attribute<TestFixtureAttribute>()
                .Property("Description").EqualTo("Integration tests"));
    
            Assert.That(typeof(AttributeConstraintExamplesFixture), Has.Attribute(typeof(CategoryAttribute))
                .Property("Name").EqualTo("Database"));
        }
    }
    

    Notes

    1. The constraint first checks that the attribute exists, then applies the chained constraint to the attribute instance.
    2. To only check for attribute existence without testing its properties, use AttributeExistsConstraint.
    3. The constraint works on both Type objects and instances (testing the instance's type).

    See Also

    • AttributeExists Constraint - Just test for attribute presence
    • Property Constraint - Test property values
    • 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