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
- The constraint first checks that the attribute exists, then applies the chained constraint to the attribute instance.
- To only check for attribute existence without testing its properties, use AttributeExistsConstraint.
- The constraint works on both
Typeobjects and instances (testing the instance's type).
See Also
- AttributeExists Constraint - Just test for attribute presence
- Property Constraint - Test property values