How to test a GraphQLHttpClient in C# with NSubtitute

97 Views Asked by At

i'm doing unit test with NUnit and NSubstitue at the project which uses a GraphQL for an API. The Client of GraphQL is an object and I try it to mock but I don't know how.

I have a Handler to override SensAsync

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
    return await Task.FromResult(_mockResponse);
}

Is there a way to easily mock the object GraphQLHttpClient in unit tests?

1

There are 1 best solutions below

2
Ivan Gechev On

You can mock an HttpClient and HttpMessageHandler instances and use them to initialize your GraphQLHttpClient. You can check the answer here.