ConfigDirector.ServerSdk.AspNetCore.Testing
1.7.1
dotnet add package ConfigDirector.ServerSdk.AspNetCore.Testing --version 1.7.1
NuGet\Install-Package ConfigDirector.ServerSdk.AspNetCore.Testing -Version 1.7.1
<PackageReference Include="ConfigDirector.ServerSdk.AspNetCore.Testing" Version="1.7.1" />
<PackageVersion Include="ConfigDirector.ServerSdk.AspNetCore.Testing" Version="1.7.1" />
<PackageReference Include="ConfigDirector.ServerSdk.AspNetCore.Testing" />
paket add ConfigDirector.ServerSdk.AspNetCore.Testing --version 1.7.1
#r "nuget: ConfigDirector.ServerSdk.AspNetCore.Testing, 1.7.1"
#:package ConfigDirector.ServerSdk.AspNetCore.Testing@1.7.1
#addin nuget:?package=ConfigDirector.ServerSdk.AspNetCore.Testing&version=1.7.1
#tool nuget:?package=ConfigDirector.ServerSdk.AspNetCore.Testing&version=1.7.1
ConfigDirector ASP.NET Core Testing
Registers a ConfigDirector test client with an ASP.NET
Core application under test, published to NuGet as ConfigDirector.ServerSdk.AspNetCore.Testing.
It targets net8.0.
An application wired up with
ConfigDirector.ServerSdk.AspNetCore builds its client
from configuration and connects during startup. This package replaces that client with the test
client's, so the application starts without an SDK key, opens no connection, and reads the values
the test seeds.
Installation
The package is released together with ConfigDirector.ServerSdk.Testing and the SDK, at the same
version, and brings the testing package along.
dotnet add package ConfigDirector.ServerSdk.AspNetCore.Testing
Test your application
using ConfigDirector.Testing;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
await using var testClient = ConfigDirectorTesting.CreateTestClient(
new Dictionary<string, object> { ["new-checkout"] = true });
using var factory = new WebApplicationFactory<Program>().WithWebHostBuilder(host =>
host.ConfigureTestServices(services => services.AddConfigDirectorTestClient(testClient)));
using var http = factory.CreateClient();
(await http.GetStringAsync("/checkout")).ShouldBe("new");
testClient.SetValue("new-checkout", false);
(await http.GetStringAsync("/checkout")).ShouldBe("classic");
AddConfigDirectorTestClient(testClient):
- Removes every
IConfigDirectorClientregistration already in the collection and registerstestClient.Clientas an instance. It therefore works fromConfigureTestServices, which runs after the application's ownAddConfigDirector. The container never disposes an instance registration; the test owns the test client and disposes it. - Supplies a placeholder SDK key when none is configured, so the startup validation of the key passes whatever the order of registrations. A configured key is left alone.
- Leaves the application's startup initialization in place, so the host calls
InitializeAsyncas in production. With no hold or failure armed, it completes at once with the stored values. WithFailInitializationandRequireReadyOnStartup, host startup throwsConfigDirectorConnectionException, as it would in production. - With
HoldInitialization, host startup waits untilCompleteInitializationor the client's timeout.WebApplicationFactory.CreateClient()blocks on host startup, so callCompleteInitializationfrom another task.
Because the test client is built outside the container, it does not see the application's
configuration: ConfigDirector:Connection:Timeout, the host's ILoggerFactory, and the AppName
and AppVersion metadata. Pass the timeout and logger factory to CreateTestClient through
TestClientOptions.
A factory shared across tests
A WebApplicationFactory shared by a test class through IClassFixture shares one test client
across its tests. Keep the test client with the factory, register it once, and call
ReplaceValues in each test's setup: it resets the values and disarms any armed hold or failure.
public sealed class CheckoutTests : IClassFixture<CheckoutTests.Factory>
{
private readonly Factory _factory;
public CheckoutTests(Factory factory)
{
_factory = factory;
_factory.TestClient.ReplaceValues(new Dictionary<string, object> { ["new-checkout"] = true });
}
public sealed class Factory : WebApplicationFactory<Program>
{
public TestClient TestClient { get; } = ConfigDirectorTesting.CreateTestClient();
protected override void ConfigureWebHost(IWebHostBuilder builder) =>
builder.ConfigureTestServices(services => services.AddConfigDirectorTestClient(TestClient));
public override async ValueTask DisposeAsync()
{
await base.DisposeAsync();
await TestClient.DisposeAsync();
}
}
}
Documentation
Full details are in the official documentation.
What changed in each release is in the changelog.
Getting Help
Reach out to us via https://www.configdirector.com/support
| Product | Versions 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. |
-
net8.0
- ConfigDirector.ServerSdk.AspNetCore (>= 1.7.0)
- ConfigDirector.ServerSdk.Testing (= 1.7.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.