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
- When no further constraint is chained,
Has.Attributecreates anAttributeExistsConstraint. - When a constraint is chained (e.g.,
.Property("Name")), it becomes an AttributeConstraint. - The constraint works on both
Typeobjects and instances (testing the instance's type).
See Also
- Attribute Constraint - Test attribute properties