Storyteller 4.2.0


Next

Async Grammars

Previous

Verifying Object State

Model In, Model Out Operations with ApiFixture Edit on GitHub


New in Storyteller 4.2 is a helper Fixture class called ApiFixture that helps for testing API's in your code that take in an input model as an argument and produce a separate resource model. This feature was built for testing HTTP API's and "request/reply" functionality in service bus applications.


public class NumbersInput
{
    public int X;
    public int Y;
}

public class NumbersOutput
{
    public int Multiplied;
    public int Added;
}

// This is the simplest possible usage that uses the default ModelFixture<NumbersInput>
// to build up the input model
public class SampleApiFixture : ApiFixture<NumbersInput, NumbersOutput>
{
    public SampleApiFixture()
    {
        Title = "Sample Api Fixture";
        InputTitle = "If the numbers are";
    }

    // This template method has to be implemented to exercise whatever the
    // underlying API is
    protected override Task<NumbersOutput> execute(NumbersInput input)
    {
        var output = new NumbersOutput
        {
            Added = input.X + input.Y,
            Multiplied = input.X * input.Y
        };

        return Task.FromResult(output);
    }
}

In usage, a specification using SampleApiFixture looks like this:

So there's a couple things going on under the covers. First, ApiFixture uses the ModelFixture functionality to build the input model in an embedded section and automatically transfer that input back to the containing ApiFixture.Model property. From there, ApiFixture itself is a CheckModelFixture that automatically adds grammars to verify public properties and fields on the resource or output type.

ApiFixture comes in two flavors. Using a custom ModelFixture<TInput> like this:


public abstract class ApiFixture<TInput, TOutput, TModelFixture> : CheckModelFixture<TOutput>
    where TModelFixture : ModelFixture<TInput>, new()
    where TInput : class
    where TOutput : class

And a simpler version for cases where the default ModelFixture<TInput> is sufficient:


public abstract class ApiFixture<TInput, TOutput> : ApiFixture<TInput, TOutput, ModelFixture<TInput>>
    where TInput : class
    where TOutput : class
{
    
}