{"slug":"contract-testing-dotnet","title":"contract-testing-dotnet","summary":"Use when the contract-testing-roster resolved a .NET stack for a provider-side contract test. Always provides the baseline WebApplicationFactory + HttpClient integration test recipe; when the Microcks opt-in is enabled, additionally provides the MicrocksContainer + TestEndpointAs","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-11-29T00:42:30.237539Z","repo":{"url":"https://github.com/SebastienDegodez/skraft-plugin","stars":17,"forks":3,"license":"GPL-3.0","updatedAt":"2026-11-29T00:18:21Z"},"bodyHtml":"<hr>\n<h2>name: contract-testing-dotnet\ndescription: Use when the contract-testing-roster resolved a .NET stack for a provider-side contract test. Always provides the baseline WebApplicationFactory + HttpClient integration test recipe; when the Microcks opt-in is enabled, additionally provides the MicrocksContainer + TestEndpointAsync(OPEN_API_SCHEMA) layer that replays the published contract against the running service and validates response codes, headers, and ProblemDetails shape. Emits test wiring only; the business TDD cycle stays with the software-engineer lead.</h2>\n<h1>Contract Testing — .NET adapter (baseline + optional Microcks)</h1>\n<p>Concrete .NET recipe for a provider-side contract test of THIS service's API.\nExtracted from the generic <code>contract-testing</code> skill so the generic skill keeps\nonly stack-agnostic authoring; this adapter owns the .NET delivery wiring.</p>\n<p>Loaded ONLY when <code>contract-testing-roster</code> resolved a .NET stack. The roster also\npasses the <code>microcks</code> opt-in flag.</p>\n<p><strong>Boundary:</strong> test wiring only. No business RED-&gt;GREEN, no Object Calisthenics,\nno consumer-side mocking. The <code>software-engineer</code> lead integrates this into its\nown TDD loop.</p>\n<h2>Layer 1 — Baseline (ALWAYS emitted)</h2>\n<p><code>WebApplicationFactory</code> + typed <code>HttpClient</code> integration test. This is produced\nwhether or not the Microcks opt-in is set.</p>\n<pre><code>public class {Api}ContractTests : IClassFixture&lt;WebApplicationFactory&lt;Program&gt;&gt;\n{\n    private readonly HttpClient _client;\n\n    public {Api}ContractTests(WebApplicationFactory&lt;Program&gt; factory)\n        =&gt; _client = factory.CreateClient();\n\n    [Fact]\n    public async Task Get_unknown_resource_returns_problem_details_404()\n    {\n        var response = await _client.GetAsync(\"/resource/does-not-exist\");\n\n        Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);\n        Assert.Equal(\"application/problem+json\",\n            response.Content.Headers.ContentType?.MediaType);\n\n        var problem = await response.Content.ReadFromJsonAsync&lt;ProblemDetails&gt;();\n        Assert.NotNull(problem);\n        Assert.Equal(404, problem!.Status);\n    }\n}\n</code></pre>\n<h2>Layer 2 — Microcks contract verification (ONLY when opt-in == true)</h2>\n<p>Stack a Microcks contract test on top of the baseline. Provider-side conformance\nis <code>MicrocksContainer.TestEndpointAsync(TestRequest)</code> with the <code>OPEN_API_SCHEMA</code>\nrunner: Microcks replays every example of the published contract against your\nRUNNING service and validates each response. Load the contract artifacts\n(<code>{api}.yaml</code> + <code>.apiexamples.yaml</code> + <code>.apimetadata.yaml</code>) authored per the\ngeneric <code>contract-testing</code> skill.</p>\n<p>NuGet: <code>Microcks.Testcontainers</code> (it brings <code>DotNet.Testcontainers</code> transitively;\ndo not also reference the unrelated <code>Testcontainers</code>/<code>TestContainers</code> packages).</p>\n<p><strong>Critical — a <code>WebApplicationFactory</code> cannot host Layer 2.</strong> It serves the app\nin-memory and exposes no TCP port the Microcks container can reach. Boot the SUT\non a real Kestrel port instead. Extract the app composition into a reusable\nfactory (e.g. <code>CheckoutHost.Create()</code> returning a <code>WebApplication</code>) so <code>Program</code>\nand this test share the exact same wiring.</p>\n<p>Real Microcks 0.3.4 API: <code>new MicrocksBuilder()...Build()</code> returns the container,\nthen <code>await container.StartAsync()</code>; <code>WithMainArtifacts(params string[])</code> loads\nthe artifacts in one call; <code>TestEndpointAsync(request)</code> is an extension method.</p>\n<pre><code>[Fact]\npublic async Task Service_satisfies_the_published_contract()\n{\n    // 1. Boot the SUT on a real port (WebApplicationFactory has none).\n    await using var app = CheckoutHost.Create();\n    app.Urls.Add(\"http://127.0.0.1:0\");\n    await app.StartAsync();\n\n    var address = app.Services.GetRequiredService&lt;IServer&gt;()\n        .Features.Get&lt;IServerAddressesFeature&gt;()!.Addresses.First();\n    var port = int.Parse(address.Split(':').Last());\n\n    // 2. Expose that host port to the Microcks container network.\n    await TestcontainersSettings.ExposeHostPortsAsync([(ushort)port]);\n\n    // 3. Start Microcks seeded from the contract artifacts.\n    var microcks = new MicrocksBuilder()\n        .WithImage(\"quay.io/microcks/microcks-uber:1.14.0-native\")\n        .WithMainArtifacts(\n            \"contracts/{api}.yaml\",\n            \"contracts/{api}.apiexamples.yaml\",\n            \"contracts/{api}.apimetadata.yaml\")\n        .Build();\n    await microcks.StartAsync();\n\n    try\n    {\n        // 4. Microcks replays every example against the running service.\n        var request = new TestRequest\n        {\n            ServiceId = \"{API Title}:1.0.0\",       // matches \"info.title:info.version\"\n            RunnerType = TestRunnerType.OPEN_API_SCHEMA,\n            TestEndpoint = $\"http://host.testcontainers.internal:{port}\",\n            Timeout = TimeSpan.FromSeconds(5),\n        };\n\n        var testResult = await microcks.TestEndpointAsync(request);\n\n        Assert.True(testResult.Success, \"Service must conform to the contract\");\n    }\n    finally\n    {\n        await microcks.DisposeAsync();\n        await app.StopAsync();\n    }\n}\n</code></pre>\n<p><code>TestEndpointAsync</code> asserts that every response code, header, and body field\n(including the <code>ProblemDetails</code> shape) matches the contract. Do NOT suppress a\nfailing <code>TestResult</code> (<code>testResult.Success == false</code>).</p>\n<blockquote>\n<p><code>VerifyAsync(name, version)</code> is a DIFFERENT method: in <code>Microcks.Testcontainers</code>\nit returns a <code>bool</code> checking how many times a MOCK was invoked — a consumer-side\nconcern. It is NOT provider conformance. Use <code>TestEndpointAsync</code> here.</p>\n</blockquote>\n<h2>Structured result back to the lead</h2>\n<p>Return, do not commit:</p>\n<pre><code>status: ok\ncapability: contract-testing\nstack: dotnet\nmicrocks: false | true\nfiles:\n  - test/{Api}.ContractTests/{Api}ContractTests.cs           # baseline (always)\n  - test/{Api}.ContractTests/{Api}ContractVerification.cs    # only when microcks == true\ntestCommand: &lt;resolved via resolving-stack-commands&gt;\nnotes: baseline WAF+HttpClient always ; Microcks TestEndpointAsync(OPEN_API_SCHEMA) added iff opt-in\n</code></pre>\n<h2>Deep-dive references (generic skill)</h2>\n<p>For contract authoring, dispatcher rules, and sample formats, see the generic\n<code>contract-testing</code> skill and its <code>references/</code>.</p>\n<h2>Rules</h2>\n<ul>\n<li>ALWAYS emit Layer 1, regardless of the opt-in.</li>\n<li>Add Layer 2 ONLY when <code>microcks: true</code>. Never replace the baseline with it.</li>\n<li>Layer 2 is <code>TestEndpointAsync(TestRequest{ OPEN_API_SCHEMA })</code> against\n<code>host.testcontainers.internal:{port}</code> — NOT <code>VerifyAsync</code> (that returns a <code>bool</code>\nasserting a mock was invoked, a consumer-side concern). Never suppress a failing\n<code>TestResult</code>.</li>\n<li>Layer 2 needs a real Kestrel port: a <code>WebApplicationFactory</code> exposes none. Boot\nthe SUT via a shared host factory and read the port from <code>IServerAddressesFeature</code>.</li>\n<li>Real API: <code>MicrocksBuilder...Build()</code> + <code>await StartAsync()</code>,\n<code>WithMainArtifacts(params string[])</code>, <code>container.TestEndpointAsync(request)</code>.\nThere is no <code>BuildAsync</code>, no singular <code>WithMainArtifact</code>.</li>\n<li>Use <code>resolving-stack-commands</code> for the test command — never hardcode <code>dotnet test</code>.</li>\n</ul>\n","files":[{"path":"SKILL.md","sizeBytes":6976,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-11-29T00:43:52.960533Z","sha256":"BDF97B6B905907E61FA6108A1ACB9120630A5952078024EEA7DC06238E74F1B2","sizeBytes":2930},"review":null,"source":{"repositoryUrl":"https://github.com/SebastienDegodez/skraft-plugin","path":"plugins/skraft-framework/skills/contract-testing-dotnet","license":"GPL-3.0","commit":"c6117c6236a25490bf1916b131866069740c9b22","subtreeSha":"7D9D25596A400C539EB59830049356A1781F3CF86AE1163D2EDBA1194C8838D1","lastSyncedAt":"2026-11-29T00:42:27.348488Z"},"reviewedAt":"2026-11-29T00:47:15.589602Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/SebastienDegodez/skraft-plugin/tree/main/plugins/skraft-framework/skills/contract-testing-dotnet"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install sebastiendegodez-skraft-plugin@llmmart"},{"target":"git","command":"git clone https://github.com/SebastienDegodez/skraft-plugin.git"}]}