Search Results for

    Show / Hide Table of Contents

    AttributeExists Constraint

    AttributeExistsConstraint tests for the existence of an attribute on a type.

    Usage

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

    Examples

    [Serializable]
    public class SerializableClass { }
    
    [Test]
    public void AttributeExistsConstraint_Examples()
    {
        // Test that a type has an attribute
        Assert.That(typeof(SerializableClass), Has.Attribute<SerializableAttribute>());
    
        // Negative test
        Assert.That(typeof(TypeConstraintSnippets), Has.No.Attribute<SerializableAttribute>());
    }
    

    Notes

    1. When no further constraint is chained, Has.Attribute creates an AttributeExistsConstraint.
    2. When a constraint is chained (e.g., .Property("Name")), it becomes an AttributeConstraint.
    3. The constraint works on both Type objects and instances (testing the instance's type).

    See Also

    • Attribute Constraint - Test attribute properties
    • 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