dennisdoomen / dennisdoomen/mockly

[API Proposal]: Make HttpMock disposable with an opt-in Strict mode (README's `using var mock` does not compile)

Open
#183 1 comment 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
40
Forks
9
Avg merge
2d 1h
Merged PRs (30d)
13

Description

## Bug

The README's headline sample and the "Power in Simplicity" section both start with:

```csharp
using var mock = new HttpMock();
```

`HttpMock` does not implement `IDisposable` (see `Mockly.ApiVerificationTests/ApprovedApi/net8.0.verified.txt`), so that snippet does not compile. Anyone copying the first sample off the README hits a compiler error immediately.

## Proposal

Rather than just dropping `using` from the docs, make `HttpMock` disposable and give disposal a purpose:

```csharp
using var mock = new HttpMock { Strict = true };
// ... test body ...
// on Dispose: throws if any configured mock was never invoked,
// or if unexpected calls were recorded
```

`Strict` would default to `false`, so nothing changes for existing users. With it on, you no longer have to remember `HaveAllRequestsCalled()` at the end of every test, and a test that forgets its assertions still fails.

## Notes

- Dispose must not throw while an exception is already in flight, or it will mask the real failure. Needs care around `Marshal.GetExceptionPointers()` / a "did the test already fail" heuristic, or a documented caveat.
- Also lets `HttpMock` dispose the `HttpClient` instances it hands out.
- Either way, the README needs fixing — if `Strict` is rejected, drop `using` from the samples.

Contributor guide

Open the contributing guide

Research direction

Start with the README samples and Mockly.ApiVerificationTests/ApprovedApi/net8.0.verified.txt, then locate HttpMock and its existing request-verification behavior. Decide whether the disposable Strict proposal is feasible, including exception-in-flight handling and HttpClient disposal; done means the API verification remains correct, strict disposal reports unmet expectations without masking test failures, and the README sample compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.