Storyteller 4.2.0


Next

Relational Databases

Previous

Extensions

ASP.Net Core Systems Edit on GitHub


Storyteller.AspNetCore is an addon Nuget library you can use to quickly add bootstrapping and system lifecycle management for ASP.Net Core applications. See Using Storyteller with ASP.Net Core Systems for a blog post that lays out the vision for this Nuget. This library heavily uses the Alba library for bootstrapping and running requests through the ASP.Net Core system.

Bootstrapping an ASP.Net Core System

The Storyteller.AspNetCore library contains a new base class called AspNetCoreSystem to simplify using Storyteller against ASP.Net Core systems. Here's a very simple, "Hello, World" level example:


public class HelloWorldSystem : AspNetCoreSystem
{
    public HelloWorldSystem()
    {

        UseStartup<Startup>();

        // No request should take longer than 250 milliseconds
        RequestPerformanceThresholdIs(250);

        // You can directly configure the CellHandling property to add lists,
        // custom conversions, or extensions
        CellHandling
            .AddSystemLevelList("states", new []{"Texas", "Missouri", "Arkansas"});
    }
}

You can apply additional configuration of your ASP.Net Core system beyond or in addition to a Startup class by using the Alba functionality for bootstrapping and configuring a running ASP.Net Core. For example, you might add additional middleware for detailed logging or override services in the application's IoC container.

IoC Container Integration

Within any Fixture in a specifications project that uses AspNetCoreSystem, the calls to Fixture.Retrieve<T>() delegates to the underlying application's IoC container.

Specification Lifecycle Hooks

In addition, there are template methods that can be overridden in a subclasses of AspNetCoreSystem for specification lifecycle events:


/// <summary>
/// Executes once before any specifications are run
/// </summary>
protected virtual void beforeAll()
{
    // Nothing
}

/// <summary>
/// Executes immediately before a specification is executed
/// </summary>
/// <param name="sut"></param>
/// <param name="context"></param>
protected virtual void beforeEach(ISystemUnderTest sut, ISpecContext context)
{
    // nothing
}

/// <summary>
/// Executes immediately after a specification is executed
/// </summary>
/// <param name="sut"></param>
/// <param name="context"></param>
protected virtual void afterEach(ISystemUnderTest sut, ISpecContext context)
{
    // nothing
}

Using AspNetCoreFixture

While you can always spin up an HttpClient instance in a Storyteller Fixture to exercise your HTTP endpoints, you can instead use Alba's Scenario support to programmatically execute HTTP requests without hitting the actual server and using its functionality to work with the response.

Let's say that we've built the obligatory "Hello, World" ASP.Net Core application. To drive that system in a Storyteller specification, we might have this method within a class that inherits from AspNetCoreFixture:


[FormatAs("The response text from {url} should be '{contents}'")]
public async Task<string> TheContentsShouldBe(string url)
{
    // Execute an Alba "Scenario"
    var result = await Scenario(_ =>
    {
        _.Get.Url(url);
    });

    return result.ResponseBody.ReadAsText().Trim();
}

Instrumentation and Logging

The AspNetCoreSystem adds some additional middleware to your configured ASP.Net Core system to record information about HTTP requests being executed during specifications. This information is exposed both in the Performance tab in the Storyteller HTML results and on an additional tab called Http Requests where more information about the request is displayed.

See Jeremy's blog post Using Storyteller with ASP.Net Core Systems for examples of this functionality, including a recipe for defining performance criteria against HTTP requests.