Skip to content

feat: add Refit.Testing package for stubbing and verifying clients#2184

Merged
glennawatson merged 4 commits into
mainfrom
feature/refit-testing
Jul 1, 2026
Merged

feat: add Refit.Testing package for stubbing and verifying clients#2184
glennawatson merged 4 commits into
mainfrom
feature/refit-testing

Conversation

@glennawatson

Copy link
Copy Markdown
Contributor

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.

Checklist

  • I have read the Contribute guide
  • Tests have been added or updated (for bug fixes / features)
  • Docs have been added or updated (for bug fixes / features)
  • Changes target the main branch
  • PR title follows Conventional Commits

Additional information

Full solution builds clean across all target frameworks and the entire test suite passes (4179 tests).

## 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

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.70%. Comparing base (533bbbe) to head (45a643b).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…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.
@sonarqubecloud

sonarqubecloud Bot commented Jul 1, 2026

Copy link
Copy Markdown

@glennawatson glennawatson merged commit 339cb8d into main Jul 1, 2026
14 checks passed
@glennawatson glennawatson deleted the feature/refit-testing branch July 1, 2026 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants