Search Results for

    Show / Hide Table of Contents

    Property Constraint

    PropertyConstraint tests for the existence of a named property on an object and then applies a constraint test to the property value.

    Constructor

    PropertyConstraint(string name, IConstraint baseConstraint)
    

    Syntax

    Has.Property(string name)... // followed by further constraint syntax
    

    Examples of Use

    public class Person
    {
        public string Name { get; set; } = "";
        public int Age { get; set; }
        public string Email { get; set; } = "";
    }
    
    [Test]
    public void PropertyConstraint_Examples()
    {
        var person = new Person { Name = "John", Age = 25, Email = "john@example.com" };
        
        Assert.That(person, Has.Property("Name").EqualTo("John"));
        Assert.That(person, Has.Property("Age").GreaterThan(18));
        Assert.That(person, Has.Property("Email").Contains("@"));
    }
    

    As shown in the example, certain common properties are known to NUnit and may be tested using a shorter form. The following properties are supported:

    Has.Length...
    Has.Count...
    Has.Message...
    Has.InnerException...
    

    See also

    • PropertyExistsConstraint
    • 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