DalSoft.RestClient.Testing

DalSoft.RestClient.Testing is a new package in its own repo that works with DalSoft.RestClient to help with unit testing and e2e testing your REST APIs.

> dotnet add package DalSoft.RestClient.Testing

It gives you two things:

  • The Verify method - fluent chained assertions on any RestClient response
  • CreateRestClient extension methods for ASP.NET Core’s in-memory TestServer and WebApplicationFactory - so you can e2e test your APIs without hosting them

The Verify method

Verify takes a type and a boolean expression - you can chain as many Verify calls as you like. Testing halts on the first failed verification, throwing a meaningful exception telling you which expression failed, for example s => s.Contains("Leanne Graham") was not verified.

[Fact]
public async Task GetUser_ProvidingAValidUserId_ReturnsExpectedResponse()
{
    var client = new RestClient("https://jsonplaceholder.typicode.com");

    await client.Resource("users/1").Get()
        .Verify<HttpResponseMessage>(response => response.IsSuccessStatusCode)
        .Verify<string>(s => s.Contains("Leanne Graham"))
        .Verify<User>(user => user.Username == "Bret")
        .Verify(o => o.username == "Bret")
        .Verify(o => o.HttpResponseMessage.IsSuccessStatusCode);
}

Notice you can Verify using a static type, a string, the HttpResponseMessage or dynamically - just like RestClient itself.

DalSoft.RestClient.Testing targets .NET Standard 2.0 - the same platforms as RestClient itself.

Next see Unit Testing and E2E Testing, or read Learn How to Test a REST API using C# and DalSoft Rest Client for a full walkthrough.

Updated: