Anarchy.Toolkit.Testing.MockServer
4.1.0
dotnet add package Anarchy.Toolkit.Testing.MockServer --version 4.1.0
NuGet\Install-Package Anarchy.Toolkit.Testing.MockServer -Version 4.1.0
<PackageReference Include="Anarchy.Toolkit.Testing.MockServer" Version="4.1.0" />
<PackageVersion Include="Anarchy.Toolkit.Testing.MockServer" Version="4.1.0" />
<PackageReference Include="Anarchy.Toolkit.Testing.MockServer" />
paket add Anarchy.Toolkit.Testing.MockServer --version 4.1.0
#r "nuget: Anarchy.Toolkit.Testing.MockServer, 4.1.0"
#:package Anarchy.Toolkit.Testing.MockServer@4.1.0
#addin nuget:?package=Anarchy.Toolkit.Testing.MockServer&version=4.1.0
#tool nuget:?package=Anarchy.Toolkit.Testing.MockServer&version=4.1.0
Anarchy.Toolkit.Testing.MockServer
Part of Anarchy I/O Toolkit, a .NET developer platform · Pillar: Infrastructure · Layer: Testing kit · Package catalog
Test helpers for Anarchy.Toolkit.Runtime.Web.MockServer: assert which requests the mock server received.
dotnet add package Anarchy.Toolkit.Testing.MockServer
MockServerClienttalks to the server's/__mockcontrol API: received requests, runtime expectations, resets.RecordedRequestAssertionsassert on what was received (ShouldHaveReceived,ShouldNotHaveReceived,ShouldAllHaveBeenMatched,AssertReceivedAsyncby method and path or by expectation id,AssertNoUnmatchedRequestsAsync), with failures listing the requests that were actually received.RecordedRequestsFile.Readreads the JSON-lines file written with--record, so you can assert after the server stopped.MockServerFixtureconnects to the server atMOCKSERVER_URL(set by the AnarchyIMockServerBuildtargets), or spawns one withMockServerProcess. RelativeExpectationPathsand globs resolve against the test output (AppContext.BaseDirectory);MockServerProcessOptionsalso setsPort,MaxLogEntries(requests kept for verification,--record-limit) andLogLevel.
When to use it
Use it in integration and end-to-end tests whose application calls HTTP services that the mock server stands in for. Most of the time the mock server is started outside the tests (by Anarchy automation or a Makefile), so tests only assert. Spawning it from the tests is there for when they run on their own, e.g. from an IDE.
How to use it
Copy the expectation folder to the test output so the spawned server finds it, whatever the working directory:
<ItemGroup>
<None Include="mocks/**" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
public sealed class IdentityMockServer : MockServerFixture
{
// Only used when MOCKSERVER_URL is not set and the fixture spawns the server itself.
protected override void Configure(MockServerProcessOptions options) => options.ExpectationPaths.Add("mocks");
}
public class UserRegistrationTests(IdentityMockServer mock) : IClassFixture<IdentityMockServer>
{
[Fact]
public async Task Register_CreatesTheUserInTheIdentityService()
{
await mock.Client.ResetAsync();
await mock.Client.AddExpectationAsync("POST", "/clients/{id}/users", 201);
// … call the application, configured to reach the identity service at mock.BaseAddress …
var requests = await mock.Client.AssertReceivedAsync("POST", "/clients/{id}/users", times: 1);
requests[0].BodyAs<CreateUserRequest>()!.Email.Should().Be("jane.doe@example.com");
await mock.Client.AssertNoUnmatchedRequestsAsync();
}
}
Paths in assertions are exact, {param} templates or regular expressions, as in expectations. MockServerProcess
runs the Anarchy.Toolkit.Runtime.Web.MockServer assembly copied next to the tests, or dotnet runs
MockServerProcessOptions.ProjectPath when you have a host of your own; it stops the server, and everything it started,
on dispose.
Releases
See the release notes for the complete release history.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Anarchy.Toolkit.Runtime.Web.MockServer (>= 1.1.0)
- Anarchy.Toolkit.Testing (>= 4.1.0)
- AwesomeAssertions (>= 9.6.0)
- Humanizer (>= 3.0.10)
- Microsoft.NET.Test.Sdk (>= 18.12.0)
- Moq (>= 4.21.0)
- Newtonsoft.Json (>= 13.0.4)
- Spectre.Console (>= 0.57.2)
- xunit.v3.extensibility.core (>= 4.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
## Releases
### 4.1.0
- `ShouldAllHaveBeenMatched` now fails on requests answered only by a match-all expectation, like `ShouldHaveNoUnmatchedRequests`.
### 4.0.0
- Add unmatched-request and expectation-ID assertions, configurable fixtures, log dumps and base-URL helpers.
- feat(Core)!: Add health and telemetry extension points
- feat(Infrastructure): Add configurable provider and messaging seams
- feat(Bootstrap)!: Classify environments safely and configure AWS secrets
- feat(Runtime)!: Add hosting health workers and UI extensibility
- feat(Testing): Extend mock server and AWS test helpers
### 3.0.0
- **Breaking:** types follow the conventions' folders and namespaces, and the package now builds on `Anarchy.Toolkit.Runtime.Web.MockServer` (formerly `Web.MockServer`).
- **Breaking:** depends on the new major versions of `Testing`.
### 2.0.1
- fix(Web): Support Windows paths and host architecture
- chore(build): Format package release notes property
### 2.0.0
- feat(automation): establish onboarding and layering conventions
- feat(telemetry)!: introduce provider-neutral application telemetry
- refactor(packages)!: restructure persistence and automation packages
- refactor(infrastructure.correlationid)!: decouple correlation scopes from Serilog
- refactor(core)!: reshape telemetry, event and health contracts
- refactor(telemetry)!: remove legacy APIs and simplify namespaces
### 1.1.0
- feat(Web.MockServer): keep an in-memory log and describe plugged-in middlewares
### 1.0.1
- docs(packages): identify each package's pillar and layer
- build(packages): attribute the copyright to Leandro Melão Medeiros
- build: relicense under Apache-2.0 and update the package metadata
- fix(mock-server): default to port 6001, which browsers allow
### 1.0.0
- Initial release.