Enhanced.Testing.Component.DbContext 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Enhanced.Testing.Component.DbContext --version 1.3.0
                    
NuGet\Install-Package Enhanced.Testing.Component.DbContext -Version 1.3.0
                    
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="Enhanced.Testing.Component.DbContext" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enhanced.Testing.Component.DbContext" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Enhanced.Testing.Component.DbContext" />
                    
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 Enhanced.Testing.Component.DbContext --version 1.3.0
                    
#r "nuget: Enhanced.Testing.Component.DbContext, 1.3.0"
                    
#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 Enhanced.Testing.Component.DbContext@1.3.0
                    
#: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=Enhanced.Testing.Component.DbContext&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Enhanced.Testing.Component.DbContext&version=1.3.0
                    
Install as a Cake Tool

Enhanced.Testing.Component

NuGet

This solution provides a set of harnesses for integrating various external components into your .NET testing environment. It simplifies the process of setting up and tearing down external dependencies such as databases, message brokers, and other services during testing.

Harnesses

The solution includes harnesses for the following components:

  • PostgreSQL: Facilitates the setup of a PostgreSQL database container for integration testing. NuGet
  • Redis: Provides support for both Redis and RedisStack, allowing for easy integration of Redis services into your tests. NuGet
  • Kafka: A harness for setting up a Kafka broker for message-based integration testing. NuGet
  • gRPC Client: Simplifies the process of testing gRPC services by providing a client harness. NuGet
  • HTTP Client: Offers a harness for testing HTTP-based services and APIs. NuGet

Getting Started

To use these testing components in your project, follow these steps:

  1. Add Dependencies: Ensure your project references the Enhanced.Testing.Component namespace and its dependencies.

  2. Configure Test Fixtures: Use the provided harnesses in your test fixtures for setting up and tearing down external services. See the SampleServiceFixture class for an example.

  3. Integration Testing: Write your integration tests using the configured fixtures to interact with real instances of your external dependencies.

Example Usage

Below is an example of how to set up a test fixture using the PostgreSQL harness:

public class MyServiceFixture : IAsyncLifetime
{
    private readonly IComponent _component;

    public MyServiceFixture()
    {
        HttpClient = new HttpClientHarness();
        PostgreSql = new PostgreSqlHarness { ConnectionName = "PeopleDb" };
        PeopleDb = new DbContextHarness<PeopleDbContext>();
        PeopleDb.EnsureCreatedOnStart(builder => builder.UseNpgsql(PostgreSql.GetConnectionString()));

        _component = ComponentBuilder.Create<Program>()
                                     .AddHarness(HttpClient)
                                     .AddHarness(PostgreSql)
                                     .AddHarness(PeopleDb)
                                     .Build();
    }

    public HttpClientHarness HttpClient { get; }

    public PostgreSqlHarness PostgreSql { get; }

    public DbContextHarness<PeopleDbContext> PeopleDb { get; }

    public Task InitializeAsync() => _component.StartAsync(CancellationToken.None);

    public Task DisposeAsync() => _component.StopAsync(CancellationToken.None);
}

Features

Real dependencies in Docker

PostgreSQL, Kafka and Redis run as real containers via Testcontainers. Set ConnectionName on a container harness and its connection string is passed to the application under test as ConnectionStrings:{ConnectionName}, so the app needs no test-specific configuration.

Managed lifecycle

StartAsync starts the harnesses in the order they were added, then the application. StopAsync stops the application first, then the harnesses in reverse order, and removes their containers. Add a harness after the harnesses it depends on (for example, DbContextHarness after PostgreSqlHarness).

Clients for the application under test

HttpClientHarness and GrpcClientHarness create clients that call the application in memory through TestServer, without opening network ports.

Test data and assertions

  • DbContextHarness<TContext> creates the schema on start (EnsureCreatedOnStart) and runs queries against the application's database (ExecuteAsync).
  • KafkaHarness creates topics on start (Topics). KafkaProducerHarness sends messages to the application, and KafkaConsumerHarness reads what it produced (SeekToEnd, ConsumeMessage).
  • RedisDatabaseHarness gives direct access to the Redis database (Database).

Configuring containers

Container harnesses let you override the image and customize the underlying Testcontainers builder:

var postgreSql = new PostgreSqlHarness
{
    Image = "postgres:17",
    ConfigureContainer = builder => builder
        .WithEnvironment("TZ", "UTC")
        .WithResourceMapping("init.sql", "/docker-entrypoint-initdb.d/")
};

Custom harnesses

Derive from Harness (override OnConfigure, OnStart, OnStop) to plug in any dependency, or from ContainerHarness<TContainer> (override OnCreateContainer, GetConnectionString) for any Testcontainers module.

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 is compatible.  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 is compatible.  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.  net11.0 is compatible. 
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
1.6.0 85 11/22/2026
1.5.0 79 11/20/2026
1.4.0 166 11/13/2026
1.3.0 140 11/12/2026
1.2.4 1,986 3/4/2026
1.2.3 410 10/18/2024
1.2.2 254 8/7/2024
1.2.1 187 8/4/2024
1.2.0 204 8/1/2024
1.1.1 245 7/22/2024
1.1.0 223 7/21/2024
1.0.1 218 7/16/2024
1.0.0 236 6/14/2024