Skip to content

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

Description

@dennisdoomen

Bug

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

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions