Search Results for

    Show / Hide Table of Contents

    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

    1. When no constraint is chained after Has.Property(), it creates a PropertyExistsConstraint.
    2. When a constraint follows (e.g., .EqualTo(5)), it becomes a PropertyConstraint.
    3. The property must be public and readable.

    See Also

    • Property Constraint - Test property values
    • 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