Lesson 40 of 93 · 6 min read · Updated
Verifying Calls with Moq: Verify, Times and VerifyNoOtherCalls
On this page
Some behavior has no return value to assert on. When a checkout succeeds, the important effect is that money is captured from the customer's card exactly once. The only way to see that effect in a unit test is to ask the payment gateway double which calls it received. Moq does this with Verify and the Times options. This lesson shows how to write a few high-value verifications and why many low-value ones make tests fragile.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- Moq 4.21.0
- FluentAssertions 7.2.2
- Microsoft.Extensions.TimeProvider.Testing 11.0.0
The Times Options
Verify takes the same kind of expression as Setup, plus a Times value that says how many matching calls are expected.
| Times | Meaning |
|---|---|
Times.Once | exactly one matching call |
Times.Never | no matching call |
Times.Exactly(n) | exactly n calls |
Times.AtLeastOnce() | one or more calls |
Times.Between(a, b, Range.Inclusive) | a count in a range |
If the count does not match, Verify throws a MockException. The message lists the expected call and every call the mock actually received, which makes failures easy to read.
One Valuable Verification: Capture Exactly Once
Double capture is a real production bug: the customer pays twice. That makes "capture was called once, with the right reference and amount" a verification worth writing.
M05.TestableDesign.Tests/MoqVerifyTests.cs
The verification checks three things at once. The reference must be the one returned by authorization, "AUTH-77", so the capture applies to the right transaction. The amount must be 500, so the customer pays what was authorized. And the count must be one. The CancellationToken is not part of this scenario, so it uses It.IsAny.
Note that CaptureAsync returns a plain Task and has no setup. A loose mock returns a completed task for it, so the service can await it without any extra configuration.
Verifying That Something Did Not Happen
"Never called" is also a contract. When the gateway declines, the service must not capture anything.
M05.TestableDesign.Tests/MoqVerifyTests.cs
Here every argument is It.IsAny. That is deliberate. With Times.Never, wide matchers make the check stronger: no capture of any kind is allowed. A narrow matcher such as "AUTH-77" would let a capture with a different reference slip through.
Mock.Of<ICouponRepository>() creates a one-line double for a dependency the test does not care about. The decline reason "Kart bloke" means "card blocked".
Performance Contracts and VerifyNoOtherCalls
Sometimes "did not call" protects performance. When no coupon code is given, the service should not touch the database.
M05.TestableDesign.Tests/MoqVerifyTests.cs
VerifyNoOtherCalls() fails if the mock received any call that was not already verified. On a repository with one method it adds little risk. On a gateway with several methods it freezes the exact set of calls, and any harmless new call breaks the test. Use it only when the whole interaction with that boundary is the contract.
Verifying the Argument That Matters
The last test checks that the repository is queried with exactly the code the customer entered.
M05.TestableDesign.Tests/MoqVerifyTests.cs
The setup is wide (It.IsAny<string>()) and the verification is narrow ("YARIYIL50"). This split gives a better failure message. If the service passed a wrong code, a narrow setup would just return null and the test would fail on the amount, far from the cause. Here the setup still answers, and Verify reports exactly which code was used. The outcome assertion on Charged.Amount comes first because the result is what the user sees.
A common mistake is to verify a call whose effect the test already proves. If the gateway returned an approval and the result is successful, AuthorizeAsync was obviously called; verifying it adds coupling and no protection. Verify only effects that leave no other observable trace, such as the capture.
Summary
Verify(expression, Times.X)checks how many matching calls a mock received; failures list the actual calls.- Verify interactions that are part of the contract, such as "capture exactly once", not every internal step.
- With
Times.Never, use wide matchers so no call of any kind can slip through. - A wide setup plus a narrow
Verifygives clearer failures than a narrow setup alone. VerifyNoOtherCalls()freezes the full set of calls; use it only when that whole set is the contract.