Skip to content

feat: let the type of a matcher select the type arguments in SetupGeneric and VerifyGeneric - #1251

Open
vbreuss wants to merge 4 commits into
mainfrom
feat/generic-matcher-type-argument
Open

vbreuss wants to merge 4 commits into
mainfrom
feat/generic-matcher-type-argument

Conversation

@vbreuss

@vbreuss vbreuss commented Dec 4, 2026

Copy link
Copy Markdown
Member

SetupGeneric and VerifyGeneric matched a parameter that depends on a type argument as object: the matcher only saw the boxed value. A typed matcher therefore also matched calls with other type arguments, and a negative matcher matched the calls of every other type:

sut.Mock.SetupGeneric.Write(It.IsAny<string>()).Throws<IOException>();
sut.Write<object>("x");   // threw, although the type argument is object
await That(sut.Mock.VerifyGeneric.Write(It.IsNot("a"))).Once();
sut.Write(42);            // counted by It.IsNot("a")

The static type of the matcher now selects the type arguments of the calls it matches, and the matcher then checks the value, the same way at the call, in VerifyGeneric and in VerifySetup. A call matches when the type that the parameter has in it converts to the type of the matcher:

  • T value: It.IsAny<string>() matches Write<string>(…) only, not Write<object>("x") or Write<int>(1). It.IsAny<Exception>() also matches Write<ArgumentException>(…), and a matcher of object (It.IsAny<object>(), It.Satisfies<object>(…)) matches every type argument.
  • Nullable value types convert as in C#: It.IsAny<int?>() matches Write<int>(…), It.IsAny<int>() does not match Write<int?>(…).
  • null matches a null argument of every type argument, It.IsNull<string>() only one of string.
  • A parameter that only partly depends on a type argument (IRequest<T>, IEnumerable<T>, T? of a value type) also takes a matcher of a type that implements or derives from it exactly, or of the underlying type, and the type arguments follow from it: Send(It.IsAny<Ping>()) with Ping : IRequest<int> matches Send<int>(ping), Store(It.IsAny<List<string>>()) matches Store<string>(list) but not Store<object>(list).
  • When overloads share a setup, the rule of the called overload applies, also for recorded calls: for Overloaded<T>(object value) next to Overloaded<T>(T value) only the value is checked.
  • Match.AnyParameters() still matches every type argument, and so does a matcher that implements IParameter<T> for several types (It.Contains).

A matcher passed as a non-generic IParameter uses the type of the IParameter<T> it implements. This changes setups and verifications that relied on a typed matcher seeing the boxed value of another type argument, e.g. a call Publish<IEvent>(orderPlaced) is no longer matched by It.IsAny<OrderPlaced>(); use It.Satisfies<object>(value => value is OrderPlaced) for them.

Not breaking against v4.2.0: logger mocks first shipped in 4.3.0-pre.1 and SetupGeneric/VerifyGeneric in 4.3.0-pre.3, so this is a behaviour change of the 4.3 pre-releases only.

@vbreuss vbreuss self-assigned this Dec 4, 2026
@vbreuss
vbreuss added this pull request to stack #1252 December 4, 2026 10:02
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Test Results

    69 files      69 suites   53m 51s ⏱️
 7 822 tests  7 812 ✅ 10 💤 0 ❌
52 928 runs  52 851 ✅ 77 💤 0 ❌

Results for commit 39c8ea3.

♻️ This comment has been updated with latest results.

@vbreuss
vbreuss force-pushed the feat/invocation-type-arguments branch from 14335f4 to 94afd69 Compare December 4, 2026 11:51
Base automatically changed from feat/invocation-type-arguments to main December 4, 2026 12:54
@vbreuss
vbreuss removed this pull request from stack #1252 December 4, 2026 12:54
@vbreuss
vbreuss force-pushed the feat/generic-matcher-type-argument branch from ff0e1e0 to c73f01e Compare December 4, 2026 13:29
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Indexer (N=1000) 105,744 B 105,745 B 🔴 +1 B (±0%)

Time

Benchmark main PR Δ
Unconfigured (N=1) 285.9 ns 254.4 ns 🟢 -11%
Unconfigured (N=10) 1.06 μs 1.18 μs 🔴 +11%
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.930 μs 0.0458 μs 0.0406 μs 1.00 5.17 KB 1.00
Imposter 2.252 μs 0.0335 μs 0.0280 μs 1.17 7.21 KB 1.39
TUnitMocks 3.663 μs 0.0224 μs 0.0187 μs 1.90 9.63 KB 1.86
Moq 879.175 μs 11.8052 μs 11.0426 μs 455.74 90.21 KB 17.44
NSubstitute 279.686 μs 3.8580 μs 3.6088 μs 144.98 53.08 KB 10.26
FakeItEasy 315.846 μs 4.0679 μs 3.6060 μs 163.73 63.94 KB 12.36
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6973P-C 3.55GHz, 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  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 23.30 ns 0.340 ns 0.318 ns 1.00 136 B 1.00
Imposter 118.45 ns 0.750 ns 0.701 ns 5.09 504 B 3.71
TUnitMocks 38.18 ns 0.622 ns 0.582 ns 1.64 216 B 1.59
Moq 1,091.20 ns 7.360 ns 6.525 ns 46.84 2088 B 15.35
NSubstitute 1,605.50 ns 4.654 ns 4.353 ns 68.92 5032 B 37.00
FakeItEasy 1,334.79 ns 5.288 ns 4.688 ns 57.30 2751 B 20.23
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.20GHz, 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  

CompleteUnconfiguredBenchmarks
Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 254.4 ns 3.65 ns 3.42 ns 1.00 1.53 KB 1.00
Imposter 1 161.5 ns 2.92 ns 2.73 ns 0.64 1.06 KB 0.69
TUnitMocks 1 231.0 ns 5.15 ns 4.82 ns 0.91 1.36 KB 0.89
Moq 1 33,291.7 ns 517.29 ns 458.57 ns 130.88 9.8 KB 6.40
NSubstitute 1 4,057.7 ns 91.39 ns 85.49 ns 15.95 10.01 KB 6.54
FakeItEasy 1 4,331.0 ns 46.97 ns 43.94 ns 17.03 11.58 KB 7.56
Mockolate 10 1,175.6 ns 10.25 ns 9.08 ns 1.00 3.95 KB 1.00
Imposter 10 700.8 ns 5.27 ns 4.40 ns 0.60 2.89 KB 0.73
TUnitMocks 10 2,171.8 ns 20.31 ns 18.00 ns 1.85 8.13 KB 2.06
Moq 10 43,462.4 ns 557.63 ns 494.33 ns 36.97 24.53 KB 6.22
NSubstitute 10 13,058.3 ns 55.75 ns 52.15 ns 11.11 22.93 KB 5.81
FakeItEasy 10 16,913.3 ns 115.71 ns 102.57 ns 14.39 53.92 KB 13.67
Mockolate 1000 93,416.0 ns 2,831.18 ns 2,648.28 ns 1.00 266.91 KB 1.00
Imposter 1000 56,415.7 ns 1,234.34 ns 1,094.21 ns 0.60 203.99 KB 0.76
TUnitMocks 1000 194,807.7 ns 2,978.45 ns 2,786.05 ns 2.09 622.59 KB 2.33
Moq 1000 828,663.5 ns 16,885.37 ns 14,968.44 ns 8.88 1653 KB 6.19
NSubstitute 1000 930,391.2 ns 21,363.11 ns 18,937.84 ns 9.97 1434.48 KB 5.37
FakeItEasy 1000 1,649,450.7 ns 29,224.48 ns 27,336.60 ns 17.67 4690.8 KB 17.57
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.19GHz, 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  

CompleteMethodBenchmarks
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 87.80 ns 2.616 ns 2.319 ns 1.00 600 B 1.00
Imposter 1 121.37 ns 5.420 ns 4.526 ns 1.38 808 B 1.35
TUnitMocks 1 172.47 ns 4.788 ns 4.245 ns 1.97 1088 B 1.81
Moq 1 2,648.77 ns 54.317 ns 50.808 ns 30.19 6552 B 10.92
NSubstitute 1 3,292.38 ns 144.799 ns 128.361 ns 37.52 9816 B 16.36
FakeItEasy 1 2,700.74 ns 51.464 ns 48.139 ns 30.78 7517 B 12.53
Mockolate 10 260.75 ns 6.647 ns 6.218 ns 1.00 1104 B 1.00
Imposter 10 286.65 ns 5.480 ns 5.126 ns 1.10 1168 B 1.06
TUnitMocks 10 912.77 ns 11.954 ns 9.982 ns 3.50 2952 B 2.67
Moq 10 4,033.75 ns 83.443 ns 69.678 ns 15.48 10536 B 9.54
NSubstitute 10 4,846.39 ns 103.019 ns 80.430 ns 18.60 12840 B 11.63
FakeItEasy 10 4,900.43 ns 110.034 ns 85.907 ns 18.80 15754 B 14.27
Mockolate 1000 16,983.93 ns 757.527 ns 671.528 ns 1.00 56544 B 1.00
Imposter 1000 14,119.49 ns 557.012 ns 521.030 ns 0.83 40768 B 0.72
TUnitMocks 1000 70,301.80 ns 1,516.714 ns 1,418.735 ns 4.15 158241 B 2.80
Moq 1000 116,671.40 ns 2,821.178 ns 2,500.901 ns 6.88 423562 B 7.49
NSubstitute 1000 155,231.02 ns 3,605.393 ns 3,196.087 ns 9.15 378536 B 6.69
FakeItEasy 1000 231,825.30 ns 5,468.703 ns 5,115.428 ns 13.67 943740 B 16.69
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.13GHz, 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  

CompletePropertyBenchmarks
Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 261.5 ns 4.53 ns 4.23 ns 1.00 928 B 1.00
Imposter 1 174.2 ns 2.56 ns 2.27 ns 0.67 672 B 0.72
TUnitMocks 1 278.7 ns 5.30 ns 4.96 ns 1.07 1096 B 1.18
Moq 1 11,162.4 ns 94.93 ns 79.27 ns 42.70 10458 B 11.27
NSubstitute 1 7,809.7 ns 132.94 ns 124.35 ns 29.88 11688 B 12.59
FakeItEasy 1 8,172.1 ns 102.95 ns 96.30 ns 31.26 11196 B 12.06
Mockolate 10 638.8 ns 8.13 ns 6.79 ns 1.00 1792 B 1.00
Imposter 10 332.8 ns 6.09 ns 5.69 ns 0.52 960 B 0.54
TUnitMocks 10 1,342.3 ns 26.55 ns 24.84 ns 2.10 3280 B 1.83
Moq 10 18,549.3 ns 118.44 ns 110.79 ns 29.04 17385 B 9.70
NSubstitute 10 18,695.3 ns 370.08 ns 346.17 ns 29.27 21553 B 12.03
FakeItEasy 10 19,699.4 ns 166.06 ns 155.33 ns 30.84 30785 B 17.18
Mockolate 1000 40,675.8 ns 697.84 ns 652.76 ns 1.00 96832 B 1.00
Imposter 1000 16,754.0 ns 266.86 ns 249.62 ns 0.41 32641 B 0.34
TUnitMocks 1000 105,345.2 ns 941.83 ns 880.99 ns 2.59 190017 B 1.96
Moq 1000 752,077.3 ns 12,720.78 ns 11,276.64 ns 18.49 724511 B 7.48
NSubstitute 1000 1,127,598.8 ns 10,566.30 ns 9,366.75 ns 27.73 1139728 B 11.77
FakeItEasy 1000 1,360,115.1 ns 14,766.05 ns 13,089.72 ns 33.45 2194291 B 22.66
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.23GHz, 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  

CompleteIndexerBenchmarks
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 511.5 ns 7.22 ns 6.76 ns 1.00 1.8 KB 1.00
Imposter 1 355.0 ns 3.74 ns 3.31 ns 0.69 1.19 KB 0.66
TUnitMocks 1 559.3 ns 6.83 ns 6.06 ns 1.09 1.7 KB 0.94
Moq 1 533,892.6 ns 1,961.09 ns 1,834.41 ns 1,043.86 37.48 KB 20.77
NSubstitute 1 213,656.5 ns 1,072.62 ns 895.68 ns 417.74 27.44 KB 15.21
FakeItEasy 1 249,202.6 ns 5,103.79 ns 4,774.08 ns 487.24 29.55 KB 16.37
Mockolate 10 1,273.5 ns 7.02 ns 6.23 ns 1.00 2.72 KB 1.00
Imposter 10 828.9 ns 9.52 ns 8.91 ns 0.65 2.24 KB 0.82
TUnitMocks 10 2,540.2 ns 34.65 ns 32.41 ns 1.99 4.7 KB 1.73
Moq 10 544,524.2 ns 2,808.65 ns 2,627.21 ns 427.59 45.84 KB 16.86
NSubstitute 10 232,105.4 ns 1,788.92 ns 1,673.36 ns 182.26 40.25 KB 14.80
FakeItEasy 10 276,909.8 ns 5,997.40 ns 5,609.97 ns 217.44 48.76 KB 17.93
Mockolate 1000 83,170.3 ns 591.43 ns 553.22 ns 1.00 103.27 KB 1.00
Imposter 1000 53,890.7 ns 450.19 ns 421.11 ns 0.65 118.26 KB 1.15
TUnitMocks 1000 186,237.1 ns 488.08 ns 432.67 ns 2.24 259.13 KB 2.51
Moq 1000 1,351,813.4 ns 3,236.85 ns 2,702.92 ns 16.25 914.36 KB 8.85
NSubstitute 1000 1,899,060.0 ns 11,275.55 ns 9,995.49 ns 22.83 1480.33 KB 14.34
FakeItEasy 1000 1,741,550.9 ns 22,707.32 ns 20,129.45 ns 20.94 2169.32 KB 21.01
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.64GHz, 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  

CompleteEventBenchmarks
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 142.2 ns 2.27 ns 2.12 ns 1.00 720 B 1.00
Imposter 167.0 ns 3.97 ns 3.71 ns 1.18 512 B 0.71
TUnitMocks 173.9 ns 5.93 ns 5.55 ns 1.22 1392 B 1.93
Moq 9,690.4 ns 51.49 ns 42.99 ns 68.18 12761 B 17.72
NSubstitute 4,065.3 ns 42.02 ns 39.31 ns 28.60 9192 B 12.77
FakeItEasy 107,994.3 ns 432.45 ns 361.12 ns 759.80 15579 B 21.64
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 2.70GHz, 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  

CallbackBenchmarks
Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 235.7 ns 3.57 ns 3.34 ns 1.00 808 B 1.00
Imposter 236.7 ns 1.06 ns 0.94 ns 1.00 616 B 0.76
TUnitMocks 322.2 ns 4.09 ns 3.62 ns 1.37 1064 B 1.32
Moq 44,639.7 ns 802.88 ns 711.73 ns 189.42 9033 B 11.18
NSubstitute 4,058.4 ns 37.95 ns 33.65 ns 17.22 7864 B 9.73
FakeItEasy 3,642.8 ns 21.35 ns 16.67 ns 15.46 6924 B 8.57
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.00GHz, 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  

AsyncBenchmarks
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 350.7 ns 11.16 ns 10.44 ns 1.00 1.01 KB 1.00
Imposter 1 443.0 ns 3.25 ns 3.04 ns 1.26 1.58 KB 1.57
TUnitMocks 1 637.2 ns 13.04 ns 12.20 ns 1.82 1.85 KB 1.84
Moq 1 336,376.3 ns 1,481.26 ns 1,236.92 ns 959.98 22.74 KB 22.56
NSubstitute 1 16,493.0 ns 188.34 ns 176.17 ns 47.07 19.28 KB 19.13
FakeItEasy 1 12,165.7 ns 76.28 ns 71.35 ns 34.72 14 KB 13.89
Mockolate 10 1,657.8 ns 22.37 ns 20.93 ns 1.00 2.98 KB 1.00
Imposter 10 1,556.4 ns 9.67 ns 8.58 ns 0.94 3.41 KB 1.14
TUnitMocks 10 4,622.5 ns 24.31 ns 21.55 ns 2.79 8.33 KB 2.80
Moq 10 385,131.9 ns 1,458.44 ns 1,364.22 ns 232.35 40.19 KB 13.50
NSubstitute 10 30,289.0 ns 93.91 ns 87.84 ns 18.27 30.6 KB 10.28
FakeItEasy 10 46,817.0 ns 462.54 ns 410.03 ns 28.24 59.08 KB 19.85
Mockolate 1000 141,664.4 ns 274.95 ns 243.74 ns 1.00 219.54 KB 1.00
Imposter 1000 121,855.7 ns 552.19 ns 516.52 ns 0.86 204.5 KB 0.93
TUnitMocks 1000 400,675.1 ns 1,694.43 ns 1,502.07 ns 2.83 555.89 KB 2.53
Moq 1000 4,492,302.6 ns 17,950.86 ns 14,989.78 ns 31.71 1891.04 KB 8.61
NSubstitute 1000 1,370,608.6 ns 8,128.00 ns 7,602.94 ns 9.68 1202.64 KB 5.48
FakeItEasy 1000 4,375,717.4 ns 55,716.03 ns 52,116.81 ns 30.89 4933.84 KB 22.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.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-v3

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

SequenceBenchmarks
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 2.635 μs 0.0251 μs 0.0235 μs 1.00 2.24 KB 1.00
Imposter 2.389 μs 0.0216 μs 0.0191 μs 0.91 2.23 KB 1.00
TUnitMocks 2.723 μs 0.0111 μs 0.0104 μs 1.03 2.58 KB 1.15
Moq 9.922 μs 0.1166 μs 0.1090 μs 3.77 10.79 KB 4.81
NSubstitute 8.505 μs 0.0971 μs 0.0909 μs 3.23 14.67 KB 6.54
FakeItEasy 8.959 μs 0.0911 μs 0.0852 μs 3.40 14.28 KB 6.37
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  

@vbreuss
vbreuss force-pushed the feat/generic-matcher-type-argument branch 2 times, most recently from 338b4d8 to 10c79f0 Compare December 4, 2026 16:49
…eric and VerifyGeneric

`SetupGeneric` and `VerifyGeneric` matched a parameter that depends on a type
argument as `object`: the matcher only saw the boxed value. A typed matcher
therefore also matched calls with other type arguments, and a negative matcher
matched the calls of every other type:

```csharp
sut.Mock.SetupGeneric.Write(It.IsAny<string>()).Throws<IOException>();
sut.Write<object>("x");   // threw, although the type argument is object
await That(sut.Mock.VerifyGeneric.Write(It.IsNot("a"))).Once();
sut.Write(42);            // counted by It.IsNot("a")
```

The static type of the matcher now selects the type arguments of the calls it
matches, and the matcher then checks the value, the same way at the call, in
`VerifyGeneric` and in `VerifySetup`. A call matches when the type that the
parameter has in it converts to the type of the matcher:

- `T value`: `It.IsAny<string>()` matches `Write<string>(…)` only, not
  `Write<object>("x")` or `Write<int>(1)`. `It.IsAny<Exception>()` also matches
  `Write<ArgumentException>(…)`, and a matcher of `object`
  (`It.IsAny<object>()`, `It.Satisfies<object>(…)`) matches every type argument.
- Nullable value types convert as in C#: `It.IsAny<int?>()` matches
  `Write<int>(…)`, `It.IsAny<int>()` does not match `Write<int?>(…)`.
- `null` matches a `null` argument of every type argument,
  `It.IsNull<string>()` only one of `string`.
- A parameter that only partly depends on a type argument (`IRequest<T>`,
  `IEnumerable<T>`, `T?` of a value type) also takes a matcher of a type that
  implements or derives from it exactly, or of the underlying type, and the type
  arguments follow from it: `Send(It.IsAny<Ping>())` with `Ping : IRequest<int>`
  matches `Send<int>(ping)`, `Store(It.IsAny<List<string>>())` matches
  `Store<string>(list)` but not `Store<object>(list)`.
- When overloads share a setup, the rule of the called overload applies, also
  for recorded calls: for `Overloaded<T>(object value)` next to
  `Overloaded<T>(T value)` only the value is checked.
- `Match.AnyParameters()` still matches every type argument, and so does a
  matcher that implements `IParameter<T>` for several types (`It.Contains`).

A matcher passed as a non-generic `IParameter` uses the type of the
`IParameter<T>` it implements. This changes setups and verifications that relied
on a typed matcher seeing the boxed value of another type argument, e.g. a call
`Publish<IEvent>(orderPlaced)` is no longer matched by `It.IsAny<OrderPlaced>()`;
use `It.Satisfies<object>(value => value is OrderPlaced)` for them.

Not breaking against v4.2.0: logger mocks first shipped in 4.3.0-pre.1 and
`SetupGeneric`/`VerifyGeneric` in 4.3.0-pre.3, so this is a behaviour change
of the 4.3 pre-releases only.
Type.GetInterfaces() on the runtime type of a matcher fails the trim analysis of the library (IL2070, IL2075), so the build broke. The built-in matchers now tell the type argument of their IParameter<T> through an internal interface, any other matcher given as a non-generic IParameter selects object, and a matcher of a type that implements the parameter type through a variant conversion now also matches.
@vbreuss
vbreuss force-pushed the feat/generic-matcher-type-argument branch from 10c79f0 to 39c8ea3 Compare December 4, 2026 18:04
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant