Mockly 1.3.0
dotnet add package Mockly --version 1.3.0
NuGet\Install-Package Mockly -Version 1.3.0
<PackageReference Include="Mockly" Version="1.3.0" />
<PackageVersion Include="Mockly" Version="1.3.0" />
<PackageReference Include="Mockly" />
paket add Mockly --version 1.3.0
#r "nuget: Mockly, 1.3.0"
#:package Mockly@1.3.0
#addin nuget:?package=Mockly&version=1.3.0
#tool nuget:?package=Mockly&version=1.3.0
About
What's this?
Mockly is a powerful and flexible HTTP mocking library for .NET that makes it easy to test code that depends on HttpClient. It provides a fluent API for configuring HTTP request mocks, capturing request details, and asserting on HTTP interactions in your tests.
The library supports:
- .NET Framework 4.7.2 and higher
- .NET 8.0 and higher
- FluentAssertions 7.x and 8.x integration for expressive test assertions
What's so special about that?
Unlike other HTTP mocking libraries, Mockly offers:
- Fluent, intuitive API - Chain method calls to build complex mocking scenarios with ease
- Wildcard pattern matching - Match URLs using wildcards (
*) in paths and query strings - Custom matchers - Use predicates for advanced request matching logic
- Request capture & inspection - Automatically capture all requests with full metadata (headers, body, timestamp)
- Powerful assertions - Built-in FluentAssertions extensions for verifying HTTP behavior
- Diagnostic support - Detailed error messages when unexpected requests occur
- Extensibility - Design allows for custom response generators and matchers
- Zero configuration - Works out of the box with sensible defaults
- Performance optimized - Regex patterns are cached for efficient matching
- Invocation limits - Restrict how many times a mock can respond using
Once(),Twice(), orTimes(n)
Who created this?
Mockly is created and maintained by Dennis Doomen, also the creator of FluentAssertions, PackageGuard, Reflectify, Pathy and the .NET Library Starter Kit. It's designed to work seamlessly with modern .NET testing practices and integrates naturally with FluentAssertions for expressive test assertions.
Key Features
🎯 Fluent Request Matching
mock.ForGet().WithPath("/api/users/*").RespondsWithJsonContent(user);
mock.ForPost().WithPath("/api/data").WithQuery("?filter=*").RespondsWithStatus(HttpStatusCode.Created);
📃 Clear Reporting
When an unexpected request occurs and there are configured mocks, Mockly helps you diagnose by reporting the closest matching mock (method, scheme/host/path/query) so you can quickly see what to adjust in your setup.
Unexpected request to:
GET http://localhost/fnv_collectiveschemes(111)
Closest matching mock:
GET https://*/fnv_collectiveschemes(123*)
Registered mocks:
- GET https://*/fnv_collectiveschemes
- POST https://*/fnv_collectiveschemes
- GET https://*/fnv_collectiveschemes(123*)
- GET https://*/fnv_collectiveschemes(123*) (1 custom matcher(s)) where (request => request.Uri?.Query == "?$count=1")
- GET https://*/fnv_collectiveschemes(456)
🔍 Request Capture & Inspection
var patches = new RequestCollection();
mock.ForPatch().WithPath("/api/update").CollectingRequestIn(patches);
// After test execution
patches.Count.Should().Be(3);
patches.First().Path.Should().Contain("/api/update");
✅ Powerful Assertions
mock.Should().HaveAllRequestsCalled();
mock.Requests.Should().NotBeEmpty();
mock.Requests.Should().NotContainUnexpectedCalls();
// Assert JSON-equivalence using a JSON string (ignores formatting/ordering)
mock.Requests.Should().ContainRequest()
.WithBodyMatchingJson("{ \"id\": 1, \"name\": \"x\" }");
// Assert the body deserializes and is equivalent to an object graph
var expected = new { id = 1, name = "x" };
mock.Requests.Should().ContainRequestForUrl("http://localhost:7021/api/*")
.WithBodyEquivalentTo(expected);
🎨 Multiple Response Types
- JSON content with automatic serialization
- Test data builder integration via
IResponseBuilder<T> - Raw string content
- Custom HTTP status codes
- Custom response generators
- OData support
🛡️ Fail-Fast Testing
mock.FailOnUnexpectedCalls = true; // Default behavior
// Throws UnexpectedRequestException if an unmocked request is made
Quick Start
Install the package:
dotnet add package mockly
To get the assertions, also install one of the two assertion packages, depending on which version of FluentAssertions you're using:
dotnet add package FluentAssertions.Mockly.v7
dotnet add package FluentAssertions.Mockly.v8
Basic usage:
using Mockly;
using FluentAssertions;
// Arrange
var mock = new HttpMock();
mock.ForGet()
.WithPath("/api/users/123")
.RespondsWithJsonContent(new { Id = 123, Name = "John Doe" });
HttpClient client = mock.GetClient();
// Act
// Note: BaseAddress defaults to https://localhost/
var response = await client.GetAsync("/api/users/123");
var content = await response.Content.ReadAsStringAsync();
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
content.Should().Contain("John Doe");
mock.Should().HaveAllRequestsCalled();
For complete documentation and advanced examples, visit dennisdoomen.github.io/mockly
Versioning
This library uses Semantic Versioning to give meaning to the version numbers. For the versions available, see the releases on this repository.
Credits
This library wouldn't have been possible without the following tools, packages and companies:
- FluentAssertions - Fluent API for asserting the results of unit tests by Dennis Doomen
- Nuke - Smart automation for DevOps teams and CI/CD pipelines by Matthias Koch
- xUnit - Community-focused unit testing tool for .NET by Brad Wilson
- Coverlet - Cross platform code coverage for .NET by Toni Solarin-Sodara
- GitVersion - From git log to SemVer in no time
- ReportGenerator - Converts coverage reports by Daniel Palme
- StyleCopyAnalyzer - StyleCop rules for .NET
- Roslynator - A set of code analysis tools for C# by Josef Pihrt
- CSharpCodingGuidelines - Roslyn analyzers by Bart Koelman to go with the C# Coding Guidelines
- Meziantou - Another set of awesome Roslyn analyzers by Gérald Barré
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- Microsoft.Extensions.Http (>= 9.0.13)
- System.Net.Http (>= 4.3.4)
- System.Text.Json (>= 8.0.6)
-
net8.0
- Microsoft.Extensions.Http (>= 9.0.13)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Mockly:
| Package | Downloads |
|---|---|
|
FluentAssertions.Mockly.v7
FluentAssertions extensions for Mockly.Http (compatible with FluentAssertions 7.x) |
|
|
FluentAssertions.Mockly.v8
FluentAssertions extensions for Mockly (compatible with FluentAssertions 8.x) |
|
|
Verify.Mockly
Adds Verify (https://github.com/VerifyTests/Verify) support for verifying Mockly (https://mockly.org/) types |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.3.0 | 182 | 2/19/2026 |
| 1.2.1 | 197 | 2/13/2026 |
| 1.2.0 | 179 | 2/8/2026 |
| 1.1.1 | 510 | 12/31/2025 |
| 1.1.0 | 215 | 12/22/2025 |
| 1.0.1 | 860 | 12/9/2025 |
| 1.0.0 | 383 | 12/8/2025 |
| 1.0.0-rc.6 | 177 | 12/7/2025 |
| 1.0.0-rc.5 | 171 | 12/7/2025 |
| 1.0.0-rc.4 | 87 | 12/6/2025 |
| 1.0.0-rc.3 | 181 | 12/4/2025 |
| 1.0.0-rc.2 | 627 | 12/2/2025 |
| 1.0.0-rc.1 | 380 | 11/30/2025 |
| 0.1.0 | 454 | 11/30/2025 |