Skip to main content

aweXpect.Mockolate

Nuget

aweXpect.Mockolate contains 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));