feat: add Refit.Testing package for stubbing and verifying clients#2184
Merged
Conversation
## What kind of change does this PR introduce?
- Feature: a new first-party `Refit.Testing` package, and the test suite migrated onto it (Moq and RichardSzalay.MockHttp removed).
## What is the new behavior?
A declarative, Refit-aware way to test the clients your app builds:
- Route table: describe expected calls as `Route` -> `Reply` entries in a collection initializer.
- Route templates mirror the interface attributes (`Route.Get("/users/{id}")`), so URLs aren't restated.
- Typed replies: `Reply.With<T>(obj)` serializes with the client's own serializer, no hand-written JSON.
- Typed request capture: read the sent body back as an object with `LastRequestBodyAsync<T>()`.
- One-line client creation: `CreateClient<T>` (reflection) and `CreateGeneratedClient<T>` (source-generated/AOT).
- `NetworkBehavior` for seeded latency and fault injection.
- `StubApiResponse<T>` for unit-testing code that consumes `IApiResponse<T>` directly.
- Verification of the calls made via `VerifyAllCalled` / `VerifyAllCalledAsync`.
Includes a usage guide under `docs/` and a README section.
## What is the current behavior?
- Tests relied on Moq and RichardSzalay.MockHttp; there was no first-party testing story for consumers.
## What might this PR break?
- None for consumers: the package is additive.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2184 +/- ##
==========================================
+ Coverage 97.59% 97.70% +0.11%
==========================================
Files 120 126 +6
Lines 6021 6327 +306
Branches 1097 1167 +70
==========================================
+ Hits 5876 6182 +306
Misses 40 40
Partials 105 105 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…face - Add coverage for every Route/Reply factory, the exact-query/body/form matchers, template placeholder edge cases, typed request capture, and StubApiResponse error accessors. - Exercise the null-URI and unbufferable-body paths via HttpMessageInvoker and a throwing content. - Exclude the Consume race guard from coverage (only reachable under a non-deterministic request race).
…tting - Cover the missing body/header/query matcher branches and the typed-capture media-type fallback so Refit.Testing is fully covered, branches included. - Revert 14 product files (and one example) that this PR had touched with whitespace only (format-run reindentation), removing pre-existing product code from the coverage patch.
- Exercise a wildcard route with a query matcher against a request with no URI (via HttpMessageInvoker), covering the null branch of the query resolver that codecov flagged as the last partial.
|
ChrisPulman
approved these changes
Jul 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What kind of change does this PR introduce?
Refit.Testingpackage, and the test suite migrated onto it (Moq and RichardSzalay.MockHttp removed).What is the new behavior?
A declarative, Refit-aware way to test the clients your app builds:
Route->Replyentries in a collection initializer.Route.Get("/users/{id}")), so URLs aren't restated.Reply.With<T>(obj)serializes with the client's own serializer, no hand-written JSON.LastRequestBodyAsync<T>().CreateClient<T>(reflection) andCreateGeneratedClient<T>(source-generated/AOT).NetworkBehaviorfor seeded latency and fault injection.StubApiResponse<T>for unit-testing code that consumesIApiResponse<T>directly.VerifyAllCalled/VerifyAllCalledAsync.Includes a usage guide under
docs/and a README section.What is the current behavior?
What might this PR break?
Checklist
mainbranchAdditional information
Full solution builds clean across all target frameworks and the entire test suite passes (4179 tests).