Skip to content

ConnectSoft.Extensions.Testing

ConnectSoft.Extensions.Testing is a NuGet package of reusable test-only helpers for:

  • Service templates that use ASP.NET Core TestServer / in-memory hosts (for example Reqnroll acceptance projects derived from Base Template, Microservice Template, Identity Template, Health Checks Aggregator).
  • ConnectSoft.Extensions.* libraries that need a small IMeterFactory in unit tests instead of copying a private TestMeterFactory type in every repo.

It is not a replacement for template-specific Reqnroll hooks, TestStartup, or domain fixtures. Those stay in each solution. This package centralizes small, stable building blocks.

Install

Use Central Package Management where possible (Directory.Packages.props):

Add a PackageVersion in Directory.Packages.props using the version published on your ConnectSoft feed (the pipeline starts at 1.0.x; pin the version your solution restores).

<PackageVersion Include="ConnectSoft.Extensions.Testing" Version="1.0.0" />

Then in the test .csproj:

<PackageReference Include="ConnectSoft.Extensions.Testing" />

The package is published to the ConnectSoft Azure Artifacts feed (same feed as other ConnectSoft.* packages). Ensure your nuget.config maps ConnectSoft.* to that source (templates-dependencies describes the template layout).

Test-only package

This NuGet contains test helpers only. Product options, metrics, and tracing samples belong in repos generated from ConnectSoft.LibraryTemplate, not in ConnectSoft.Extensions.Testing.

API overview

Namespace / type Purpose
ConnectSoft.Extensions.Testing.Http.TestServerForwardingHandler Forwards HttpRequestMessage through an HttpClient (typically TestServer.CreateClient()). Use when a health check or SDK needs an HttpMessageHandler but traffic must go through the in-memory pipeline.
ConnectSoft.Extensions.Testing.Metrics.TestMeterFactory Minimal Microsoft.Extensions.Diagnostics.IMeterFactory for unit tests.
ConnectSoft.Extensions.Testing.Hosting.TestHostDisposeHelper Disposes HttpClient then IHost (sync or async) to reduce ordering bugs in acceptance harness teardown.

Example: forwarding handler

using ConnectSoft.Extensions.Testing.Http;

// After TestServer is available:
var innerClient = server.CreateClient();
innerClient.BaseAddress = new Uri(BeforeAfterTestRunHooks.BaseUrlConstant);
var handler = new TestServerForwardingHandler(innerClient, disposeClient: true);
var typedClient = new SomeSdkClient(handler); // or IHttpClientFactory-style wiring

Example: test meter factory

using ConnectSoft.Extensions.Testing.Metrics;
using Microsoft.Extensions.Diagnostics;

IMeterFactory meterFactory = new TestMeterFactory();
using var meter = meterFactory.Create(new MeterOptions("MyTests.Metrics"));
var counter = meter.CreateCounter<long>("operations.total");
counter.Add(1);

See also