Skip to content

fix: verify an omitted optional parameter against its declared default in classic mode - #1027

Merged
vbreuss merged 1 commit into
mainfrom
fix/classic-verify-omitted-optional
Nov 18, 2026
Merged

vbreuss merged 1 commit into
mainfrom
fix/classic-verify-omitted-optional

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 18, 2026

Copy link
Copy Markdown
Member

With the classic overloads (C# 14 or MockolateDisableUnionParameters, and the members that keep them in union mode: overloaded, generic and params methods), setup and verify disagreed about an optional parameter that is left out or given a null matcher. The setup maps it to the declared default value, but the verification compared with default(T), so the verification of a call that happened failed and its message showed an empty argument:

public interface IOpt { void Opt(string? s, int number = 5); }

sut.Opt("a");
sut.Mock.Verify.Opt(It.IsAny<string?>()).Once();
// before: Expected that mock invoked method Opt(It.IsAny<string>(), ) exactly once, but it never did.

The generated verify method now replaces a null matcher of an optional parameter with a LiteralParameterMatch<T> for the declared default value before it builds the verification (number ??= new LiteralParameterMatch<int>(5);). Because this happens up front, the typed fast path, the mixed literal/matcher overloads and the predicate path (generic methods, more than four parameters) all match the declared default, for methods and delegates, and the failure message prints it like a literal value (Opt(It.IsAny<string>(), 5)). It allocates one matcher, as the It.Is<T>(default!) it replaces did. The union overloads already used the declared default and are unchanged.

Behavior change: in a classic verification an explicit null matcher for an optional parameter now also means the declared default value instead of default(T), exactly as in the setup and as the parameter matching page documents. For a parameter whose declared default is default(T) only the failure message changes (0 instead of an empty argument for an int).

…t in classic mode

With the classic overloads (C# 14 or `MockolateDisableUnionParameters`, and the members that keep them in union mode: overloaded, generic and `params` methods), setup and verify disagreed about an optional parameter that is left out or given a `null` matcher. The setup maps it to the declared default value, but the verification compared with `default(T)`, so the verification of a call that happened failed and its message showed an empty argument:

```csharp
public interface IOpt { void Opt(string? s, int number = 5); }

sut.Opt("a");
sut.Mock.Verify.Opt(It.IsAny<string?>()).Once();
// before: Expected that mock invoked method Opt(It.IsAny<string>(), ) exactly once, but it never did.
```

The generated verify method now replaces a `null` matcher of an optional parameter with a `LiteralParameterMatch<T>` for the declared default value before it builds the verification (`number ??= new LiteralParameterMatch<int>(5);`). Because this happens up front, the typed fast path, the mixed literal/matcher overloads and the predicate path (generic methods, more than four parameters) all match the declared default, for methods and delegates, and the failure message prints it like a literal value (`Opt(It.IsAny<string>(), 5)`). It allocates one matcher, as the `It.Is<T>(default!)` it replaces did. The union overloads already used the declared default and are unchanged.

Behavior change: in a classic verification an explicit `null` matcher for an optional parameter now also means the declared default value instead of `default(T)`, exactly as in the setup and as the parameter matching page documents. For a parameter whose declared default is `default(T)` only the failure message changes (`0` instead of an empty argument for an `int`).
@vbreuss vbreuss self-assigned this Nov 18, 2026
@vbreuss
vbreuss enabled auto-merge (squash) November 18, 2026 15:40
@vbreuss vbreuss added the bug Something isn't working label Nov 18, 2026
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Test Results

    47 files      47 suites   59m 16s ⏱️
 4 826 tests  4 826 ✅ 0 💤 0 ❌
48 694 runs  48 694 ✅ 0 💤 0 ❌

Results for commit 9b346f3.

@vbreuss
vbreuss disabled auto-merge November 18, 2026 16:23
@vbreuss
vbreuss merged commit 258e88d into main Nov 18, 2026
24 checks passed
@vbreuss
vbreuss deleted the fix/classic-verify-omitted-optional branch November 18, 2026 16:23
@github-actions

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main @​ 7467af5d: memory to the latest run, time to the median of the last 10 runs, as the runners differ in speed. A relative change marks when Mockolate gets more than 15% faster or slower relative to Imposter and TUnitMocks: 🔴 / 🟢 against both of them, 🔸 / 🔹 against only one.

CallbackBenchmarks

✅ No changes in memory or relative performance (1 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Callback 209.0 ns → 200.7 ns (-4%) 792 B → 792 B 0.99× → 1.03× 2.34× → 2.53×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 2.87GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 200.7 ns 3.21 ns 3.00 ns 1.00 792 B 1.00
Imposter 206.1 ns 2.59 ns 2.43 ns 1.03 616 B 0.78
TUnitMocks 507.0 ns 6.61 ns 6.18 ns 2.53 2056 B 2.60
Moq 78,811.5 ns 410.91 ns 320.81 ns 392.71 9056 B 11.43
NSubstitute 4,447.7 ns 37.23 ns 34.82 ns 22.16 7864 B 9.93
FakeItEasy 4,289.7 ns 47.38 ns 42.00 ns 21.37 6930 B 8.75

CompleteEventBenchmarks

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Event 1.13× → 1.17× 1.12× → 1.33× 🔹 -16%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Event 159.0 ns → 201.0 ns (+26%) 696 B → 696 B 1.13× → 1.17× 1.12× → 1.33× 🔹 -16%
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon Platinum 8370C CPU 2.80GHz (Max: 3.39GHz), 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 201.0 ns 3.09 ns 2.89 ns 1.00 696 B 1.00
Imposter 236.1 ns 2.58 ns 2.41 ns 1.17 512 B 0.74
TUnitMocks 267.6 ns 7.14 ns 6.33 ns 1.33 1392 B 2.00
Moq 14,794.6 ns 164.85 ns 146.14 ns 73.61 12761 B 18.33
NSubstitute 5,867.4 ns 38.80 ns 36.30 ns 29.19 9192 B 13.21
FakeItEasy 189,185.0 ns 2,224.82 ns 1,857.82 ns 941.29 15619 B 22.44

CompleteIndexerBenchmarks

✅ No changes in memory or relative performance (3 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Indexer (N=1) 411.4 ns → 421.9 ns (+3%) 1,648 B → 1,648 B 0.68× → 0.72× 1.59× → 1.74×
Indexer (N=10) 1.22 μs → 1.30 μs (+6%) 2,584 B → 2,584 B 0.54× → 0.56× 1.82× → 1.85×
Indexer (N=1000) 86.58 μs → 95.72 μs (+11%) 105,545 B → 105,545 B 0.48× → 0.49× 1.93× → 1.87×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.24GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 421.9 ns 5.08 ns 4.24 ns 1.00 1.61 KB 1.00
Imposter 1 302.2 ns 2.92 ns 2.73 ns 0.72 1.07 KB 0.67
TUnitMocks 1 732.9 ns 9.68 ns 9.06 ns 1.74 2.72 KB 1.69
Moq 1 227,026.3 ns 1,471.93 ns 1,376.84 ns 538.12 20.32 KB 12.62
NSubstitute 1 10,404.3 ns 75.58 ns 70.70 ns 24.66 12.78 KB 7.94
FakeItEasy 1 10,948.1 ns 50.90 ns 47.61 ns 25.95 13.27 KB 8.25
Mockolate 10 1,299.2 ns 14.32 ns 13.39 ns 1.00 2.52 KB 1.00
Imposter 10 732.6 ns 7.35 ns 6.51 ns 0.56 2.13 KB 0.84
TUnitMocks 10 2,408.1 ns 22.50 ns 21.05 ns 1.85 5.37 KB 2.13
Moq 10 239,159.9 ns 563.04 ns 526.67 ns 184.11 29.69 KB 11.77
NSubstitute 10 25,111.6 ns 175.25 ns 163.93 ns 19.33 25.58 KB 10.14
FakeItEasy 10 23,465.0 ns 129.36 ns 121.00 ns 18.06 32.47 KB 12.87
Mockolate 1000 95,723.3 ns 712.04 ns 594.58 ns 1.00 103.07 KB 1.00
Imposter 1000 46,840.0 ns 423.74 ns 396.37 ns 0.49 118.15 KB 1.15
TUnitMocks 1000 178,737.3 ns 1,011.59 ns 946.24 ns 1.87 293.67 KB 2.85
Moq 1000 1,031,580.4 ns 7,754.97 ns 6,475.75 ns 10.78 1006.46 KB 9.76
NSubstitute 1000 1,637,522.1 ns 10,310.97 ns 9,140.41 ns 17.11 1465.65 KB 14.22
FakeItEasy 1000 1,439,570.4 ns 41,867.78 ns 39,163.15 ns 15.04 2153 KB 20.89

CompleteMethodBenchmarks

✅ No changes in memory or relative performance (3 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Method (N=1) 136.1 ns → 106.7 ns (-22%) 600 B → 600 B 1.16× → 1.33× 3.13× → 3.33×
Method (N=10) 330.0 ns → 296.8 ns (-10%) 1,104 B → 1,104 B 0.94× → 1.01× 3.62× → 3.77×
Method (N=1000) 22.81 μs → 20.96 μs (-8%) 56,544 B → 56,544 B 0.78× → 0.82× 3.77× → 3.83×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 106.7 ns 1.93 ns 1.80 ns 1.00 600 B 1.00
Imposter 1 142.1 ns 1.60 ns 1.34 ns 1.33 728 B 1.21
TUnitMocks 1 355.6 ns 3.11 ns 2.91 ns 3.33 2088 B 3.48
Moq 1 114,050.4 ns 838.57 ns 784.40 ns 1,068.75 14914 B 24.86
NSubstitute 1 4,655.8 ns 21.75 ns 19.28 ns 43.63 9320 B 15.53
FakeItEasy 1 4,286.8 ns 47.44 ns 42.06 ns 40.17 8160 B 13.60
Mockolate 10 296.8 ns 2.94 ns 2.75 ns 1.00 1104 B 1.00
Imposter 10 299.3 ns 3.75 ns 3.51 ns 1.01 1088 B 0.99
TUnitMocks 10 1,118.2 ns 6.56 ns 6.14 ns 3.77 3840 B 3.48
Moq 10 119,256.5 ns 748.71 ns 663.72 ns 401.86 18906 B 17.12
NSubstitute 10 6,801.5 ns 31.74 ns 29.69 ns 22.92 12344 B 11.18
FakeItEasy 10 6,612.6 ns 104.24 ns 92.40 ns 22.28 16394 B 14.85
Mockolate 1000 20,958.1 ns 43.87 ns 34.25 ns 1.00 56544 B 1.00
Imposter 1000 17,129.8 ns 113.01 ns 100.18 ns 0.82 40688 B 0.72
TUnitMocks 1000 80,371.9 ns 527.91 ns 440.83 ns 3.83 203585 B 3.60
Moq 1000 296,743.7 ns 4,795.55 ns 4,485.76 ns 14.16 431938 B 7.64
NSubstitute 1000 228,889.4 ns 2,433.99 ns 2,276.75 ns 10.92 378037 B 6.69
FakeItEasy 1000 290,748.6 ns 4,241.67 ns 3,967.66 ns 13.87 944402 B 16.70

CompletePropertyBenchmarks

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Property (N=10) 0.52× → 0.62× 🔹 -16% 2.47× → 2.79×
Property (N=1000) 0.40× → 0.54× 🔹 -26% 2.79× → 3.24×
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Property (N=1) 230.2 ns → 158.1 ns (-31%) 904 B → 904 B 0.67× → 0.69× 1.73× → 1.75×
Property (N=10) 562.7 ns → 383.2 ns (-32%) 1,768 B → 1,768 B 0.52× → 0.62× 🔹 -16% 2.47× → 2.79×
Property (N=1000) 37.37 μs → 27.20 μs (-27%) 96,808 B → 96,808 B 0.40× → 0.54× 🔹 -26% 2.79× → 3.24×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.52GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 158.1 ns 2.31 ns 2.16 ns 1.00 904 B 1.00
Imposter 1 108.3 ns 1.86 ns 1.74 ns 0.69 672 B 0.74
TUnitMocks 1 276.8 ns 4.49 ns 3.75 ns 1.75 1672 B 1.85
Moq 1 5,126.8 ns 172.38 ns 161.25 ns 32.43 10568 B 11.69
NSubstitute 1 3,466.2 ns 29.85 ns 26.46 ns 21.93 11688 B 12.93
FakeItEasy 1 3,507.0 ns 25.53 ns 21.32 ns 22.19 11196 B 12.38
Mockolate 10 383.2 ns 2.58 ns 2.15 ns 1.00 1768 B 1.00
Imposter 10 236.7 ns 2.53 ns 2.37 ns 0.62 960 B 0.54
TUnitMocks 10 1,068.1 ns 8.72 ns 7.28 ns 2.79 4024 B 2.28
Moq 10 8,322.6 ns 98.92 ns 92.53 ns 21.72 18505 B 10.47
NSubstitute 10 8,385.1 ns 169.76 ns 158.79 ns 21.89 21553 B 12.19
FakeItEasy 10 9,048.1 ns 57.56 ns 53.84 ns 23.62 30785 B 17.41
Mockolate 1000 27,196.0 ns 526.84 ns 467.03 ns 1.00 96808 B 1.00
Imposter 1000 14,804.0 ns 94.81 ns 79.17 ns 0.54 32641 B 0.34
TUnitMocks 1000 88,192.4 ns 489.47 ns 408.73 ns 3.24 242489 B 2.50
Moq 1000 360,131.2 ns 3,783.95 ns 3,354.37 ns 13.25 836538 B 8.64
NSubstitute 1000 540,575.5 ns 10,803.46 ns 10,105.56 ns 19.88 1139725 B 11.77
FakeItEasy 1000 640,752.4 ns 4,333.74 ns 4,053.78 ns 23.57 2194324 B 22.67

CompleteUnconfiguredBenchmarks

Memory

Benchmark main PR Δ
Unconfigured (N=1000) 273,265 B 273,264 B 🟢 -1 B (±0%)

Relative performance (how many times as long the other library takes as Mockolate, main → PR)

Benchmark vs Imposter vs TUnitMocks
Unconfigured (N=1) 0.54× → 0.64× 🔹 -16% 1.05× → 1.20×
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Unconfigured (N=1) 402.5 ns → 368.2 ns (-9%) 1,512 B → 1,512 B 0.54× → 0.64× 🔹 -16% 1.05× → 1.20×
Unconfigured (N=10) 1.49 μs → 1.47 μs (-2%) 3,984 B → 3,984 B 0.54× → 0.60× 1.78× → 1.89×
Unconfigured (N=1000) 126.20 μs → 108.95 μs (-14%) 273,265 B → 273,264 B 0.55× → 0.64× 2.07× → 2.35×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.68GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 368.2 ns 7.61 ns 7.12 ns 1.00 1.48 KB 1.00
Imposter 1 234.3 ns 1.98 ns 1.85 ns 0.64 1.06 KB 0.72
TUnitMocks 1 441.6 ns 4.91 ns 4.36 ns 1.20 1.81 KB 1.23
Moq 1 70,676.7 ns 327.65 ns 290.45 ns 192.04 9.79 KB 6.63
NSubstitute 1 5,753.5 ns 17.91 ns 15.88 ns 15.63 10.01 KB 6.78
FakeItEasy 1 5,824.6 ns 43.96 ns 41.12 ns 15.83 11.58 KB 7.84
Mockolate 10 1,466.5 ns 11.72 ns 10.97 ns 1.00 3.89 KB 1.00
Imposter 10 874.1 ns 5.65 ns 5.01 ns 0.60 2.89 KB 0.74
TUnitMocks 10 2,768.6 ns 16.04 ns 14.22 ns 1.89 8.47 KB 2.18
Moq 10 82,415.3 ns 407.05 ns 339.91 ns 56.20 24.53 KB 6.30
NSubstitute 10 17,137.3 ns 143.95 ns 134.65 ns 11.69 22.93 KB 5.89
FakeItEasy 10 22,188.8 ns 365.03 ns 341.44 ns 15.13 53.92 KB 13.86
Mockolate 1000 108,953.6 ns 362.69 ns 302.86 ns 1.00 266.86 KB 1.00
Imposter 1000 69,712.0 ns 919.03 ns 814.70 ns 0.64 203.99 KB 0.76
TUnitMocks 1000 256,363.7 ns 4,928.77 ns 4,369.22 ns 2.35 691.21 KB 2.59
Moq 1000 1,194,077.4 ns 7,543.88 ns 6,687.45 ns 10.96 1653.01 KB 6.19
NSubstitute 1000 1,245,403.1 ns 6,436.13 ns 5,705.46 ns 11.43 1434.49 KB 5.38
FakeItEasy 1000 2,290,007.9 ns 69,232.61 ns 64,760.23 ns 21.02 4690.81 KB 17.58

CompleteWorkflowBenchmarks

✅ No changes in memory or relative performance (1 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Complete 967.6 ns → 962.1 ns (-1%) 2,872 B → 2,872 B 0.96× → 0.98× 2.90× → 2.89×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.74GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 962.1 ns 7.03 ns 6.24 ns 1.00 2.8 KB 1.00
Imposter 946.9 ns 12.76 ns 11.93 ns 0.98 3.17 KB 1.13
TUnitMocks 2,776.8 ns 37.74 ns 33.46 ns 2.89 10.3 KB 3.67
Moq 200,768.2 ns 725.60 ns 566.50 ns 208.68 46.39 KB 16.54
NSubstitute 19,329.5 ns 229.28 ns 203.25 ns 20.09 37.13 KB 13.24
FakeItEasy 15,944.2 ns 354.03 ns 331.16 ns 16.57 34.27 KB 12.22

MockCreationBenchmarks

✅ No changes in memory or relative performance (1 benchmarks)

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
CreateMock 16.3 ns → 15.5 ns (-5%) 136 B → 136 B 5.66× → 5.86× 1.70× → 1.82×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.59GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 11.0.0 (11.0.0, 11.0.26.53018), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 15.52 ns 0.393 ns 0.328 ns 1.00 136 B 1.00
Imposter 90.85 ns 1.811 ns 1.694 ns 5.86 504 B 3.71
TUnitMocks 28.23 ns 1.096 ns 1.025 ns 1.82 216 B 1.59
Moq 1,301.27 ns 11.537 ns 10.792 ns 83.90 2088 B 15.35
NSubstitute 1,884.43 ns 26.935 ns 25.195 ns 121.49 5032 B 37.00
FakeItEasy 1,639.47 ns 37.514 ns 33.255 ns 105.70 2755 B 20.26

github-actions Bot added a commit that referenced this pull request Nov 18, 2026
…er against its declared default in classic mode (#1027) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 18, 2026
…er against its declared default in classic mode (#1027) by Valentin Breuß
@github-actions

Copy link
Copy Markdown

This is addressed in release v4.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released The issue is released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant