Code Monkey home page Code Monkey logo

solid.testing's Introduction

Solid.Testing License Build status Solid.Testing on nuget

Solid.Testing is a library used for integration testing and system testing of web apis. It's been designed to be used with AspNetCore and ASP.Net OWIN. It uses Solid.Http internally to perform HTTP requests to an in memory host.

Packages

Usage

TestingServer is a class that wraps an in-memory instance of your api, whether it's AspNetCore 3.1 or ASP.Net OWIN. A random port is chosen for the in-memory host and a client set up internally for communication. Once you have an instance of the TestingServer, you can perform requests and assert them using a fluid interface.

[Fact]
public async Task ShouldRespondWithTwoValues()
{
    // This '_server' member is an instance of TestingServer
    await _server
        // This is the fluent interface from Solid.Http 
        .GetAsync("values")
        .WithHeader("my-header", "my-header-value")

        // This is the fluent interface from Solid.Testing
        .ShouldRespondSuccessfully()
        .Should(async response =>
        {
            using(var stream = await response.ReadAsStreamAsync())
            {
                var values = JsonSerializer.Deserialize<IEnumerable<string>>(stream);
                // We use xUnit internally, so we use it for our examples. However, any unit test framework can work.
                Assert.Collection(
                  values,
                  value => Assert.Equal("value1", value),
                  value => Assert.Equal("value2", value)
                );
            }
        })
    ;
}

Assertion methods

Methods that extend Solid.Testing.Models.Assertion or Solid.Http.ISolidHttpRequest start with the Should prefix. For convenience, they are all in the Solid.Http namespace.

Building the TestingServer

To build a TestingServer, you need to use the TestingServerBuilder. There are different extension methods for AspNetCore and ASP.Net OWIN. It's also possible to extend this to another self-hosted http framework.

AspNetCore

The basic TestingServer builder method is pretty simple.

> dotnet add package Solid.Testing.AspNetCore
private TestingServer BuildTestingServer()
{
    return new TestingServerBuilder()
        .AddAspNetCoreHostFactory()
        // This is the startup class for your AspNetCore application
        .AddStartup<Startup>()
        .Build()
    ;
}

AspNetCore with https

You can also have a TestingServer which hosts the service with https.

> dotnet add package Solid.Testing.AspNetCore.Extensions.Https
private TestingServer BuildTestingServer()
{
    return new TestingServerBuilder()
        .AddAspNetCoreHttpsHostFactory()
        // This is the startup class for your AspNetCore application
        .AddStartup<Startup>()
        .Build()
    ;
}

ASP.Net OWIN

If you use ASP.Net OWIN, there is an extension for the TestingServerBuilder that will host your service.

> dotnet add package Solid.Testing.Owin
private TestingServer BuildTestingServer()
{
    return new TestingServerBuilder()
        .AddOwinHostFactory()
        // This is the startup class for your ASP.Net OWIN application
        .AddStartup<Startup>()
        .Build()
    ;
}

Adding more advanced customization

There are multiple things that you can do to change the TestingServer. You can add services which it uses internally. These could, for example, be the services that Solid.Http is using for communication.

private TestingServer BuildTestingServer()
{
    return new TestingServerBuilder()
        .AddAspNetCoreHostFactory(webHostBuilder =>
        {
            webHostBuilder.ConfigureAppConfiguration((context, configurationBuilder) =>
            {
                var configuration = new Dictionary<string, string>()
                {
                    { "My__Configuration__Key", "myvalue"}
                };
                // Add custom configuration for your AspNetCore application.
                configurationBuilder.AddInMemoryCollection(configuration);
            });
        })
        .AddTestingServices(services => 
        {
            services.AddSingleton<IHttpClientFactory, MyCustomHttpClientFactory>();
            services.ConfigureSolidHttp(builder =>
            {
                // Use Newtonsoft.Json instead of System.Text.Json
                // This is in the Solid.Http.NewtonsoftJson package
                builder.AddNewtonsoftJson();
            });
        })
        // This is the startup class for your AspNetCore application
        .AddStartup<Startup>()
        .Build()
    ;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.