Skip to content

DSL wrapper around .NET test WebApplicationFactory to simplify integration tests in aspnetcore using F#

License

Notifications You must be signed in to change notification settings

jkone27/fsharp-integration-tests

Repository files navigation

ApiStub.FSharp NuGet Badge 🦔

alt text

JUST_STOP_OIL
Stand With Ukraine
Ceasefire Now

Easy API Testing 🧞‍♀️

This library makes use of F# computation expressions 🪔✨ to hide some complexity of WebApplicationFactory and provide the user with a domain specific language (DSL) for integration tests.

An "antique" C# API (👴🏽🦖🦕) is also available since v.1.1 for enhanced accessibility 😺.

Documentation

Access the documentation website for more info on how to use this library.

Scenario

sequenceDiagram
    participant TestWebAppFactoryBuilder as Test
    participant MainApp as App
    participant DependencyApp as Dep

    TestWebAppFactoryBuilder->>MainApp: GET /Hello
    MainApp->>DependencyApp: GET /externalApi
    DependencyApp-->>MainApp: Response
    MainApp-->>TestWebAppFactoryBuilder: Response

Loading

Test 🧪

using F#

open ApiStub.FSharp.CE
open ApiStub.FSharp.BuilderExtensions
open ApiStub.FSharp.HttpResponseHelpers
open Xunit

module Tests =

    // build your aspnetcore integration testing CE
    let test = new TestWebAppFactoryBuilder<Program>()

    [<Fact>]
    let ``Calls Hello and returns OK`` () = task {

        let client = 
            test { 
                GETJ "/externalApi" {| Ok = "yeah" |}
            }
            |> _.GetFactory()
            |> _.CreateClient()

        let! r = client.GetAsync("/Hello")

        // rest of your tests...

    }

or in C# if you prefer

using ApiStub.FSharp;
using Xunit;
using static ApiStub.Fsharp.CsharpExtensions; 

public class Tests 
{
    [Fact]
    async Task CallsHelloAndReturnsOk() 
    {

        var client = 
            new CE.TestWebAppFactoryBuilder<Web.Sample.Program>()
                .GETJ("/externalApi", new { Ok = "Yeah" })
                .GetFactory()
                .CreateClient();

        var r = await client.GetAsync("/Hello");

        // rest of your tests...
    }
}

Test .NET C# 🤝 from F#

F# is a great language, but it doesn't have to be scary to try it. Integration and Unit tests are a great way to introduce F# to your team if you are already using .NET or ASPNETCORE.

In fact you can add an .fsproj within a C# aspnetcore solution .sln, and just have a single F# assembly test your C# application from F#, referencing a .csproj file is easy! just use regular dotnet add reference command.

How to Contribute ✍️

  • Search for an open issue or report one, and check if a similar issue was reported first
  • feel free to get in touch, to fork and check out the repo
  • test and find use cases for this library, testing in F# is awesome!!!!

Commit linting 📝

This project uses Commitlint npm package and ConventionalCommits specification for commits, so be aware to follow them when committing, via Husky.NET

Versioning 📚

This repository uses Versionize as a local dotnet tool to version packages when publishing. Versionize relies on conventional commits to work properly.

References