Skip to content

docs: clarify AttributeConstraint works on ICustomAttributeProvider, not arbitrary instances#1189

Merged
OsirisTerje merged 3 commits into
masterfrom
copilot/fix-attribute-constraint-issue
Jul 3, 2026
Merged

docs: clarify AttributeConstraint works on ICustomAttributeProvider, not arbitrary instances#1189
OsirisTerje merged 3 commits into
masterfrom
copilot/fix-attribute-constraint-issue

Conversation

Copilot AI commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

The docs incorrectly stated that AttributeConstraint and AttributeExistsConstraint work on "both Type objects and instances (testing the instance's type)." In reality, they work on any object implementing ICustomAttributeProvider — passing an arbitrary class instance that doesn't implement it throws an ArgumentException.

Changes

  • AttributeConstraint.md — Replace note 3 with accurate description: the constraint works on ICustomAttributeProvider implementers (Type, MethodInfo, Assembly, etc.), and explicitly notes that passing an arbitrary instance results in an error.
  • AttributeExistsConstraint.md — Same correction applied to note 3.

The previously documented behavior would lead users to expect this to work:

// Fails — TestClassWithAttribute does not implement ICustomAttributeProvider
Assert.That(new TestClassWithAttribute(), Has.Attribute<CustomAttribute>());

// Works — typeof() returns a Type, which implements ICustomAttributeProvider
Assert.That(typeof(TestClassWithAttribute), Has.Attribute<CustomAttribute>());

// Also works — MethodInfo implements ICustomAttributeProvider
Assert.That(GetType().GetMethod(nameof(MyTest)), Has.Attribute<TestAttribute>());

Copilot AI linked an issue Jul 3, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix attribute constraint not working on all instances docs: clarify AttributeConstraint works on ICustomAttributeProvider, not arbitrary instances Jul 3, 2026
Copilot AI requested a review from stevenaw July 3, 2026 20:14
Comment thread docs/articles/nunit/writing-tests/constraints/AttributeConstraint.md Outdated
Comment thread docs/articles/nunit/writing-tests/constraints/AttributeExistsConstraint.md Outdated
Co-authored-by: Steven Weerdenburg <stevenaw@users.noreply.github.com>
@stevenaw
stevenaw marked this pull request as ready for review July 3, 2026 20:22
Copilot AI requested a review from stevenaw July 3, 2026 20:22
@OsirisTerje
OsirisTerje merged commit 5b673c3 into master Jul 3, 2026
7 checks passed
@OsirisTerje
OsirisTerje deleted the copilot/fix-attribute-constraint-issue branch July 3, 2026 21:31
github-actions Bot pushed a commit that referenced this pull request Jul 3, 2026
…issue

docs: clarify AttributeConstraint works on ICustomAttributeProvider, not arbitrary instances 5b673c3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attribute constraint doesn't work on all instances

3 participants