Skip to main content

HotChocolate GraphQL–Integration tests

The original way I wrote integration tests for my GraphQL endpoints was inspired by this blog post: https://chillicream.com/blog/2019/04/11/integration-tests

As explained in this post you can create a schema instance and directly invoke it:

However this approach no longer worked after upgrading to HotChocolate 12.  It started to fail with the following error message:

Autofac.Core.Registration.ComponentNotRegisteredException : The requested service 'HotChocolate.ISchema' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

I tried multiple approaches to get rid of this error but after a few attempts I rewrote the tests to use the Microsoft.AspNetCore.Mvc.Testing package.

I copied a few Helper classes that I found in the HotChocolate repo to make this easier in combination with GraphQL.

Now I could write the same test like this: