Shane32.TestHelpers 2.0.1

dotnet add package Shane32.TestHelpers --version 2.0.1
                    
NuGet\Install-Package Shane32.TestHelpers -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shane32.TestHelpers" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shane32.TestHelpers" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Shane32.TestHelpers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shane32.TestHelpers --version 2.0.1
                    
#r "nuget: Shane32.TestHelpers, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Shane32.TestHelpers@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shane32.TestHelpers&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Shane32.TestHelpers&version=2.0.1
                    
Install as a Cake Tool

Shane32.TestHelpers

NuGet Coverage Status

This library contains a set of helper classes and methods to facilitate the testing of GraphQL applications.

GraphQLTestBase

This class is a base class for testing GraphQL queries and mutations. Use the generic parameter to specify the startup class of the application to be tested. If a serverconfig.json file is found as an embedded resource within the test project, it will be used to configure the test server as if it was an appsettings.json file.

Sample use with SQLite EF Core database:

/// <summary>
/// Represents the base class for GraphQL testing.
/// </summary>
public class TestBase : GraphQLTestBase<Startup>
{
    public TestBase() : base()
    {
        // set the default role
        Claims.Set("role", "All.Admin");
    }

    /// <summary>
    /// Gets the test database context.
    /// </summary>
    protected TestDbContext Db => (TestDbContext)Services.GetRequiredService<AppDbContext>();

    /// <inheritdoc/>
    protected override void ConfigureWebHostBuilder(IWebHostBuilder webHostBuilder)
    {
        // prep environment:
        //   set environment name to "Test"
        //   use the serverconfig.json embedded resource as the configuration source, if found
        //   run the Startup class's Configure and ConfigureServices methods
        //   allow unsigned JWT tokens and do not verify the issuer or audience
        base.ConfigureWebHostBuilder(webHostBuilder);

        // apply additional configuration below, which applies after the base configuration
        webHostBuilder
            .ConfigureTestServices(services => {
                // reconfigure database connection to use the SQLite in-memory database
                services.AddSingleton(_ => {
                    var c = new SqliteConnection("Data Source=:memory:");
                    c.Open();
                    return c;
                });
                services.AddSingleton<AppDbContext>(provider => {
                    var db = new TestDbContext(provider.GetRequiredService<SqliteConnection>());
                    db.Database.EnsureCreated();
                    db.ResetDbTrace();
                    return db;
                });
            });
    }

    /// <inheritdoc/>
    public override Task<ExecutionResponse> RunQueryAsync(string query, object? variables = null) => RunQueryAsync(query, variables, true);

    /// <inheritdoc cref="RunQueryAsync(string, object?)"/>
    /// <param name="noTracking">Whether to clear the change tracker before and after running the query.</param>
    public async Task<ExecutionResponse> RunQueryAsync(string query, object? variables = null, bool noTracking = true)
    {
        if (noTracking)
            Db.ChangeTracker.Clear();
        var response = await base.RunQueryAsync(query, variables);
        if (noTracking)
            Db.ChangeTracker.Clear();
        return response;
    }
}

Sample test:

public class Class1 : TestBase
{
    [Fact]
    public async Task Typename()
    {
        var response = await RunQueryAsync("""
            query {
              __typename
            }
            """);
        response.ShouldMatchApproved();
    }
}

Credits

Glory to Jehovah, Lord of Lords and King of Kings, creator of Heaven and Earth, who through his Son Jesus Christ, has reedemed me to become a child of God. -Shane32

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 766 7/7/2025
2.0.0 184 7/7/2025
1.0.0 4,374 6/30/2025