Skip to main content
net8.0net10.0.NETStandard2.0

Expectations to verify interactions with mocks from Mockolate.

Published
Mar 22, 2026
7mo ago
Total Downloads
25.5K
25,454

Install

dotnet add package aweXpect.Mockolate --version 2.0.0-pre.1
ProductVersions
.NET
net10.0net8.0
.NET Standard
netstandard2.0

README

aweXpect.Mockolate

Changelog

Nuget Coverage Mutation testing badge

Expectations to verify interactions with mocks from Mockolate.

Features

Interaction count

Verify that a method was called a specific number of times:

var sut = IMyService.CreateMock();
sut.MyMethod();

await That(sut.Mock.Verify.MyMethod()).Once();             // Exactly once
await That(sut.Mock.Verify.MyMethod()).Twice();            // Exactly twice
await That(sut.Mock.Verify.MyMethod()).Never();            // Never called
await That(sut.Mock.Verify.MyMethod()).AtLeastOnce();      // At least once
await That(sut.Mock.Verify.MyMethod()).AtLeastTwice();     // At least twice
await That(sut.Mock.Verify.MyMethod()).AtLeast(3.Times()); // At least 3 times
await That(sut.Mock.Verify.MyMethod()).AtMostOnce();       // At most once
await That(sut.Mock.Verify.MyMethod()).AtMostTwice();      // At most twice
await That(sut.Mock.Verify.MyMethod()).AtMost(4.Times());  // At most 4 times
await That(sut.Mock.Verify.MyMethod()).Exactly(2.Times()); // Exactly 2 times
Asynchronous verification

With Within(TimeSpan timeout), you can check whether the expected number of calls occurred within a given time interval. This is useful for asynchronous or delayed invocations in the background.

var sut = IMyService.CreateMock();

// Start asynchronous calls, e.g., in a Task
Task.Run(async () =>
{
    await Task.Delay(500);
    sut.MyMethod();
});

// Verifies that MyMethod was called at least once within 1 second
await That(sut.Mock.Verify.MyMethod())
    .AtLeastOnce()
    .Within(TimeSpan.FromSeconds(1));

Instead of a fixed time span, you can also provide a CancellationToken to limit how long the verification should wait for the expected interactions:

var token = new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token;

// Verifies that MyMethod was called at least once within 1 second
await That(sut.Mock.Verify.MyMethod())
    .AtLeastOnce()
    .WithCancellation(token);

Interaction order

Verify that methods were called in a specific sequence:

var sut = IMyService.CreateMock();
sut.MyMethod(1);
sut.MyMethod(2);
sut.MyMethod(3);
sut.MyMethod(4);

// Verifies MyMethod(1), then MyMethod(2), then MyMethod(4) were called in order
await That(sut.Mock.Verify.MyMethod(It.Is(1))).Then(
    m => m.MyMethod(It.Is(2)),
    m => m.MyMethod(It.Is(4))
);

Additional Verifications

All interactions are verified

With AllInteractionsAreVerified you can check whether all interactions with the mock have actually been verified. This helps to detect unintended or forgotten calls.

var sut = IMyService.CreateMock();
sut.MyMethod(1);
sut.MyMethod(2);

await That(sut.Mock.Verify.MyMethod(It.IsAny<int>())).AtLeastOnce();
 // Succeeds, because the verification applies to both method calls.
await That(sut.Mock.Verify).AllInteractionsAreVerified();
All setups are used

With AllSetupsAreUsed you can check whether all defined setups on the mock have actually been used. This ensures that no setup configurations remain unused.

var sut = IMyService.CreateMock();
sut.Mock.Setup.MyMethod(It.Is(1)).Returns(10);
sut.Mock.Setup.MyMethod(It.Is(2)).Returns(20);

sut.MyMethod(1);

// Fails, because the setup for MyMethod(2) was never used.
await That(sut.Mock.Verify).AllSetupsAreUsed();

Web Extensions

JSON Content

You can precisely verify JSON content in HTTP requests during your tests. This feature is especially useful for testing HTTP clients and web APIs.

// Verifies that a request was sent with a JSON body equivalent to { "foo": 1, "bar": "baz" }
httpClient.Mock.Setup
    .PostAsync(It.IsAny<Uri>(), It.IsHttpContent().WithJsonMatching(new { foo = 1, bar = "baz" }))
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

// You can also provide a string representation of the JSON, and it ignores formatting differences or property order
httpClient.Mock.Setup
    .PostAsync(It.IsAny<Uri>(), It.IsHttpContent().WithJson("{\"bar\": \"baz\", \"foo\": 1}"))
    .ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

Changes from Previous Version

No structural changes between v1.3.0 and v2.0.0-pre.1.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

This version is marked prerelease. Validate its API and upgrade path before production use.Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.