Consider this rest api call.
app.MapGet("/getorder/{id}",
async (Guid id, [FromServices] TTService service) => await service.GetOrder(fileid, ct));
GetOrder has this interface, returning an Order object.
Order GetOrder(Guid id)
Then we have a client that calls the API
Order GetOrder(Guid id)
{
return await _client.Request("getorder", id).GetJsonAsync<Order>();
}
So we can simply call
Order o = client.GetOrder(new Guid("..."));
A unit test that tests that client around that.
[Theory]
[MemberData(nameof(GetOrderSearchData))]
public void Test_OrderSearch_OrderFound(string[] inputs)
{
var order = new OrderFaker().UseSeed(1).Generate();
_api.GetOrder(order.ID).Returns(order);
_page.SetOrderSearchValue(input);
_page.OrderInfo.OrderNumber.Should().Be(order.OrderNumber);
}
So far so good.
Now somewhere down the line the service interface was changed that it did not return a single Order anymore but an IEnumerable.
IEnumerable<Order> GetOrder(Guid id)
The service was updated but not the client, that still expected a single Order.
Code committed, selenium tests succeeded and deployed.
But in testing the call failed with could not serialize json error as the Order collection could not be serialized to a single Order of course.
So how can we write unit tests that check that the interface of a service matches that of a client?