Storyteller 5.1.0


Next

Verifying Object State

Previous

Creating Complex Objects

Configuring Input Models with ModelFixture Edit on GitHub


Storyteller 4.2 added a new Fixture type called ModelFixture<T> that can be used to set up complex object models necessary for test data input in your specifications. This feature is closely related to Creating Complex Objects (and shares some code).

Using the inevitable Address model, we might have a fixture like this:


public class AddressModelFixture : ModelFixture<Address>
{
    public AddressModelFixture()
    {
        Title = "Address Model Setup";

        // The For() statement is a helper to customize the editing
        // or rendering of the Cell's for the public properties or fields
        For(x => x.City)
            .SelectionValues("Austin", "Round Rock", "Cedar Park");
    }
}

By default, the ModelFixture<T> class will generate grammars to set the values of all public properties or fields where Storyteller "knows" how to convert a raw string into that type. If a property or field type is another model object, ModelFixture<T> will add grammars for the deeper properties. To make that concrete, say you have an Address model like this:


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;
}

In the case above, ModelFixture<Address> would add a grammar to set the value of the Address.Area.Name property.

Do note that you can add any other kind of grammar to your ModelFixture<T> for more complex object setup.

Attaching the Model

ModelFixture<T> has a property named Model that stores the model object being configured. If your model type has a public, no argument constructor, ModelFixture<T> will automatically create one at the beginning of each usage of that fixture. Otherwise, you would need an additional grammar to attach the model:


public class SpecialModel
{
    public string Name { get; }

    public SpecialModel(string name)
    {
        Name = name;
    }
}

public class SpecialModelFixture : ModelFixture<SpecialModel>
{
    [FormatAs("For the name {name}")]
    public void ForName(string name)
    {
        Model = new SpecialModel(name);
    }
}