contract-testing-dotnet
Concrete .NET adapter for a provider-side contract test: Layer 1 (WebApplicationFactory) is always emitted; Layer 2 (Microcks TestEndpointAsync) is additive when the opt-in is set.
When to use
- When contract-testing-roster resolved
.NETas the stack - Emitting the baseline integration test (Layer 1 — always required)
- Stacking the Microcks contract-verification layer (Layer 2 — only when
microcks: true)
Entry contract
- Stack resolved as .NET by contract-testing-roster
-
Microcks opt-in flag: false(default)true - Contract artifacts (
{api}.yaml+.apiexamples.yaml+.apimetadata.yaml) when opt-in istrue
Exit contract
- Layer 1 (always):
WebApplicationFactory<Program>+ typedHttpClientintegration test asserting status code, content type, andProblemDetailsshape on error paths - Layer 2 (opt-in only):
MicrocksContainer+TestEndpointAsync(TestRequest{ OPEN_API_SCHEMA })againsthost.testcontainers.internal:{port} - Structured result block:
stack,microcks,files[],testCommand
Invariants
- Layer 1 is ALWAYS emitted — regardless of the opt-in flag
- Layer 2 is ADDITIVE — never replaces Layer 1, never emitted without it
- Layer 2 needs a real Kestrel port —
WebApplicationFactoryexposes no TCP port; boot the SUT via a shared host factory and read the port fromIServerAddressesFeature TestEndpointAsyncnotVerifyAsync—VerifyAsyncreturns aboolasserting a mock was hit (consumer-side); never use it for provider conformance- Never suppress a failing
TestResult—Assert.True(testResult.Success, ...)must not be removed or skipped - Real API —
MicrocksBuilder...Build()+await StartAsync();WithMainArtifacts(params string[]); noBuildAsync(), no singularWithMainArtifact - Test command via resolving-stack-commands — never hardcode
dotnet test
Why this shape
An in-memory test server validates the application wiring without coupling to a live dependency. Stacking a Microcks contract layer on top replays the published contract examples against the running service, making conformance a deterministic gate.
« Replace a component that the SUT depends on with a test-specific equivalent. » — Meszaros, G., xUnit Test Patterns, 2007.
Allowed customisation
- Microcks image in
MicrocksBuilder.WithImage(...)(L1) - Exposed port via
TestcontainersSettings.ExposeHostPortsAsync(...)(L1) - Shared fixture via
MicrocksFixture+ICollectionFixture(L2)
See also
- contract-testing — Generic contract authoring (OpenAPI, Microcks samples, artifact bridging)
- contract-testing-roster — Resolves this adapter and the opt-in flag
- resolving-stack-commands — Resolves the test command
- software-engineer — DELIVER agent that triggers this adapter