Verifying Object State Edit on GitHub


If you need to assert the values of several object properties in one logical step, the Fixture.VerifyPropertiesOf<T>() method is a helper to quickly put together a paragraph grammer to verify multiple properties.

Using the same Address type from the previous section (Configuring Input Models with ModelFixture), you could build a grammar like this one:


public IGrammar TheAddressShouldBe()
{
    return VerifyPropertiesOf<Address>("then the new address properties should be", x =>
    {
        x.Check(o => o.Address1);
        x.Check(o => o.Address2);
        x.Check(o => o.City);
    });
}

In usage, a specification using that grammar will render results like this below:

You can use multiple "dots" in the call to Check(expression) and even verify the result of a method with no arguments. So Check(o => o.Property1.Property2.Property3) or Check(o => o.IsActive()) are both valid. As of now, only properties and methods are supported. Support for fields will be added later.

CheckModelFixture

New in Storyteller 4.2 is a helper Fixture class called CheckModelFixture that smoothes out a little bit of friction in creating a Fixture that needs to make several assertions against the state of a model object.

Here's an example against a simple Address DTO:


public class ModelForwardingFixture : CheckModelFixture<Address>
{
    public ModelForwardingFixture()
    {
        Title = "Build and Verify Address";

        // You can customize the Cell editing and appearance
        // of any of the automatically generated grammars by
        // property or field name. The expression can be deep as
        // well
        For(x => x.City).Header("The city");

        // The Model property is an easy way to set the current Address model
        // that should be verified by all the children grammars
        Model = new Address();
    }

    public IGrammar BuildAddress()
    {
        // This is a new 4.2 helper to build a model object
        // with an embedded section and use that object within
        // the containing Fixture
        return Build<Address>("If the address is")
            .With<AddressModelFixture>()
            .Forward(a => Model = a);
    }
}

Without doing anything else, CheckModelFixture<T> will add grammars to verify the values of all public properties or fields of the model type ("T"). To some degree, CheckModelFixture<T> can also guess at deeper properties or fields on intermediate objects. For example, if your Address had an Area property that in turn exposed properties:


public class Address
{
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string StateOrProvince { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
    public string TimeZone { get; set; }

    public double DistanceFromOffice { get; set; }

    public string[] Names { get; set; }

    public string County;

    public Area Area { get; set; }
}

public class Area
{
    public string Name;
}

CheckModelFixture<Address> would be smart enough to add a grammar to check the value of the Address.Area.Name property. Do note that the auto-discovery of grammar's does apply to Array types, but any property or field that returns a different enumerable of any kind is not automatically used. There's nothing stopping you from adding additional grammars to your own CheckModelFixture<T> to verify child collections of the model.