PropertyExists Constraint
PropertyExistsConstraint tests for the existence of a named property on an object.
Usage
Has.Property(string propertyName)
Examples
[Test]
public void PropertyExistsConstraint_Examples()
{
// Check that property exists
Assert.That("Hello", Has.Property("Length"));
Assert.That(new List<int>(), Has.Property("Count"));
// Negative test
Assert.That("Hello", Has.No.Property("NonExistent"));
// Check that multiple properties exist
var person = new Person { Name = "Alice", Age = 30 };
Assert.That(person, Has.Property("Name").And.Property("Age"));
}
Notes
- When no constraint is chained after
Has.Property(), it creates aPropertyExistsConstraint. - When a constraint follows (e.g.,
.EqualTo(5)), it becomes a PropertyConstraint. - The property must be public and readable.
See Also
- Property Constraint - Test property values