1

Using Hot Chocolate, how do I integration test the implementation of a ObjectFieldDescriptorAttribute?

Given the following implementation:

public class ValidApiKeyAttribute : ObjectFieldDescriptorAttribute
{
    public override void OnConfigure(IDescriptorContext descriptorContext, IObjectFieldDescriptor descriptor, MemberInfo member)
    {
        descriptor
            .Use(next => async context =>
            {
                var httpContextAccessor = context.Services.GetService(typeof(IHttpContextAccessor)) 
                    as IHttpContextAccessor;
                var authenticationSettings = context.Services.GetService(typeof(IOptions<AuthenticationSettings>)) 
                    as IOptions<AuthenticationSettings>;

                // --

                await next(context);
            });
    }
}

Which is called from a Query class

public class Query
{
    [ValidApiKey]
    public IQueryable<Something> GetSomething()
    {
      // --
    }
}

How do I unit test the middleware? I assume I need to mock the FieldDelegate and the IObjectFieldDescriptor? This is what I've got so far:

    [Fact]
    public async Task Query_GetSomething_Success()
    {
        // arrange
        var request = QueryRequestBuilder.New()
            .SetQuery("")
            .Create();
        
       //? var fieldDelegate = new FieldDelegate(async (context) => await Task.CompletedTask);

        var executor = await new ServiceCollection()
            .AddGraphQL()
            .AddType<Query>()
            .BuildRequestExecutorAsync();

        // act and assert
        var result = await executor.ExecuteAsync(request);            
    }
sjokkogutten
  • 2,005
  • 2
  • 21
  • 24

0 Answers0