Skip to content

fix: mock generic types that are closed over dynamic - #1026

Merged
vbreuss merged 1 commit into
mainfrom
fix/generic-interface-closed-over-dynamic
Nov 18, 2026
Merged

vbreuss merged 1 commit into
mainfrom
fix/generic-interface-closed-over-dynamic

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 18, 2026

Copy link
Copy Markdown
Member

Mocking a generic type that is closed over dynamic, such as IRepo<dynamic>.CreateMock(), generated code that did not compile in classic and in union mode: a type constructed with dynamic can neither be implemented nor be a constraint (CS1966, CS1968), and the mock named the mocked type in both positions (the mock class, IMockSetup<T>, IMockVerify<T>, the constraints of As<T> and Implementing<T>). This also affected class and delegate mocks (Base<dynamic>, Factory<dynamic>), nested arguments (IRepo<List<dynamic>>) and .Implementing<IRepo<dynamic>>(). #982 only covered members typed as dynamic.

IRepo<dynamic> and IRepo<object> are identity-convertible, so the generator now names the mocked type, its inherited types and the containing type of every member with object in place of dynamic at any depth, by replacing the dynamic part of the symbol display. The mock is therefore the mock of IRepo<object>, which is assignable to IRepo<dynamic>, and it is also named after object (Mock.IRepo_object, "IRepo mock"). The types of the members are emitted as before.

Because both spellings now produce the same CreateMock and Implementing extensions, the collected mocks are deduplicated by their emitted type names instead of by equality. Otherwise mocking IRepo<dynamic> and IRepo<object> in one compilation would emit two mocks that differ only in how they spell nested member types (List<dynamic> and List<object>) and make the calls ambiguous (CS0121, CS0111).

Example:

public interface IRepo<T>
{
    T Get(T key);
    void Add(T item);
}

IRepo<dynamic> sut = IRepo<dynamic>.CreateMock();
sut.Mock.Setup.Get("key").Returns("value");

object value = sut.Get("key");
sut.Add(42);

sut.Mock.Verify.Add(42).Once();

@vbreuss vbreuss self-assigned this Nov 18, 2026
@vbreuss vbreuss added the bug Something isn't working label Nov 18, 2026
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Test Results

    47 files  ±  0      47 suites  ±0   1h 7m 16s ⏱️ +28s
 4 892 tests + 15   4 892 ✅ + 15  0 💤 ±0  0 ❌ ±0 
49 305 runs  +124  49 305 ✅ +124  0 💤 ±0  0 ❌ ±0 

Results for commit 7f18f76. ± Comparison against base commit 0806680.

This pull request removes 22 and adds 31 tests. Note that renamed tests count towards both.
 x-myHeader3: baz, True)
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(bool Equals(object obj);
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.SourceGenerators.Tests.MethodSetupsTests ‑ WhenImplementingObjectMethods_ShouldUseSpecialInterfaces(string ToString();
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
Mockolate.Tests.Web.ItExtensionsTests+IsHttpContentTests+WithHeadersTests ‑ ShouldVerifyStringHeaders(x-myHeader1 : foo
…
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.Internal.Tests.Internals.WildcardTests ‑ ShouldSupportMultilineStrings(
)
Mockolate.SourceGenerators.Tests.ClassicVerifyMessageTests ‑ Verify_InClassicMode_WithTypesClosedOverDynamic_ShouldCompileMatchAndDescribeArguments
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ BothSpellings_InDifferentCombinations_ShouldCompileAndShareTheMocks(CSharp14)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ BothSpellings_InDifferentCombinations_ShouldCompileAndShareTheMocks(CSharp15)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ BothSpellings_ShouldGenerateTheSameSourcesInAnyOrder(CSharp14)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ BothSpellings_ShouldGenerateTheSameSourcesInAnyOrder(CSharp15)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ ClassImplementingTheAdditionalInterface_ShouldShareTheMembersOfBothSpellings(CSharp14)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ ClassImplementingTheAdditionalInterface_ShouldShareTheMembersOfBothSpellings(CSharp15)
Mockolate.SourceGenerators.Tests.DynamicTypeArgumentTests ‑ TypeNamedDynamic_ShouldBeKeptAsTypeArgument
…

♻️ This comment has been updated with latest results.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main @​ 0806680c: 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 204.2 ns → 200.3 ns (-2%) 792 B → 792 B 0.98× → 0.99× 2.45× → 2.21×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.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-v3

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

Callback Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 200.3 ns 3.05 ns 2.85 ns 1.00 792 B 1.00
Imposter 197.6 ns 1.07 ns 0.89 ns 0.99 616 B 0.78
TUnitMocks 442.7 ns 3.88 ns 3.62 ns 2.21 2056 B 2.60
Moq 104,291.5 ns 840.37 ns 744.97 ns 520.72 9056 B 11.43
NSubstitute 4,715.7 ns 51.21 ns 47.91 ns 23.55 7864 B 9.93
FakeItEasy 4,540.4 ns 36.43 ns 34.08 ns 22.67 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.17× → 1.19× 1.15× → 0.96× 🔸 +19%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Event 138.8 ns → 125.0 ns (-10%) 696 B → 696 B 1.17× → 1.19× 1.15× → 0.96× 🔸 +19%
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  

Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 125.0 ns 2.63 ns 2.33 ns 1.00 696 B 1.00
Imposter 149.0 ns 3.66 ns 3.42 ns 1.19 512 B 0.74
TUnitMocks 120.6 ns 1.17 ns 0.98 ns 0.97 1392 B 2.00
Moq 10,493.3 ns 150.91 ns 133.78 ns 83.97 12985 B 18.66
NSubstitute 4,010.4 ns 91.69 ns 81.28 ns 32.09 9192 B 13.21
FakeItEasy 195,627.4 ns 879.77 ns 822.94 ns 1,565.42 15618 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) 403.5 ns → 442.3 ns (+10%) 1,648 B → 1,648 B 0.69× → 0.67× 1.68× → 1.63×
Indexer (N=10) 1.17 μs → 1.25 μs (+7%) 2,584 B → 2,584 B 0.57× → 0.54× 2.00× → 1.95×
Indexer (N=1000) 79.89 μs → 93.47 μs (+17%) 105,545 B → 105,545 B 0.52× → 0.46× 2.12× → 2.00×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
INTEL XEON PLATINUM 8573C 3.53GHz, 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  

Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 442.3 ns 13.81 ns 12.92 ns 1.00 1.61 KB 1.00
Imposter 1 295.8 ns 7.75 ns 6.87 ns 0.67 1.07 KB 0.67
TUnitMocks 1 722.6 ns 13.32 ns 12.46 ns 1.63 2.72 KB 1.69
Moq 1 92,171.1 ns 379.79 ns 296.51 ns 208.56 20.32 KB 12.63
NSubstitute 1 8,675.9 ns 81.89 ns 72.59 ns 19.63 12.78 KB 7.94
FakeItEasy 1 6,867.5 ns 98.35 ns 87.19 ns 15.54 13.27 KB 8.24
Mockolate 10 1,253.9 ns 8.50 ns 7.53 ns 1.00 2.52 KB 1.00
Imposter 10 671.9 ns 15.11 ns 14.14 ns 0.54 2.13 KB 0.84
TUnitMocks 10 2,444.4 ns 52.45 ns 49.06 ns 1.95 5.37 KB 2.13
Moq 10 98,014.6 ns 355.06 ns 296.49 ns 78.17 29.67 KB 11.76
NSubstitute 10 20,089.1 ns 98.38 ns 76.81 ns 16.02 25.58 KB 10.14
FakeItEasy 10 16,017.5 ns 228.50 ns 213.74 ns 12.77 32.47 KB 12.87
Mockolate 1000 93,465.2 ns 1,105.47 ns 923.12 ns 1.00 103.07 KB 1.00
Imposter 1000 43,325.9 ns 979.42 ns 916.15 ns 0.46 118.14 KB 1.15
TUnitMocks 1000 186,867.8 ns 1,871.46 ns 1,659.00 ns 2.00 293.67 KB 2.85
Moq 1000 696,209.9 ns 5,134.18 ns 4,802.51 ns 7.45 1006.36 KB 9.76
NSubstitute 1000 1,274,795.5 ns 7,681.62 ns 7,185.39 ns 13.64 1465.53 KB 14.22
FakeItEasy 1000 1,055,721.0 ns 9,770.06 ns 9,138.92 ns 11.30 2153 KB 20.89

CompleteMethodBenchmarks

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

Benchmark vs Imposter vs TUnitMocks
Method (N=1) 1.19× → 1.09× 3.26× → 2.83× 🔸 +15%
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Method (N=1) 137.6 ns → 148.6 ns (+8%) 600 B → 600 B 1.19× → 1.09× 3.26× → 2.83× 🔸 +15%
Method (N=10) 327.4 ns → 375.9 ns (+15%) 1,104 B → 1,104 B 0.95× → 0.89× 3.71× → 3.48×
Method (N=1000) 22.67 μs → 25.98 μs (+15%) 56,544 B → 56,544 B 0.80× → 0.77× 3.97× → 3.66×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.60GHz, 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  

Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 148.6 ns 3.49 ns 3.27 ns 1.00 600 B 1.00
Imposter 1 162.7 ns 2.82 ns 2.64 ns 1.10 728 B 1.21
TUnitMocks 1 420.5 ns 4.99 ns 4.16 ns 2.83 2088 B 3.48
Moq 1 196,151.9 ns 1,634.15 ns 1,528.59 ns 1,320.68 14902 B 24.84
NSubstitute 1 5,847.2 ns 23.78 ns 21.08 ns 39.37 9320 B 15.53
FakeItEasy 1 5,841.8 ns 68.31 ns 60.56 ns 39.33 8160 B 13.60
Mockolate 10 375.9 ns 2.46 ns 2.18 ns 1.00 1104 B 1.00
Imposter 10 336.3 ns 3.85 ns 3.60 ns 0.89 1088 B 0.99
TUnitMocks 10 1,306.3 ns 18.32 ns 17.13 ns 3.48 3840 B 3.48
Moq 10 201,035.4 ns 977.05 ns 866.13 ns 534.87 18901 B 17.12
NSubstitute 10 8,848.2 ns 51.77 ns 48.43 ns 23.54 12345 B 11.18
FakeItEasy 10 9,360.0 ns 119.62 ns 106.04 ns 24.90 16393 B 14.85
Mockolate 1000 25,984.3 ns 127.31 ns 112.85 ns 1.00 56544 B 1.00
Imposter 1000 19,965.0 ns 95.46 ns 89.29 ns 0.77 40688 B 0.72
TUnitMocks 1000 95,226.2 ns 1,126.67 ns 998.77 ns 3.66 203585 B 3.60
Moq 1000 380,320.0 ns 1,790.55 ns 1,674.88 ns 14.64 431938 B 7.64
NSubstitute 1000 270,204.8 ns 1,249.85 ns 1,043.68 ns 10.40 378038 B 6.69
FakeItEasy 1000 362,833.3 ns 5,046.58 ns 4,473.66 ns 13.96 944362 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.59× → 0.26× 🔴 +127% 2.71× → 1.31× 🔴 +106%
Property (N=1000) 0.49× → 0.42× 🔸 +16% 3.04× → 3.01×
All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Property (N=1) 248.0 ns → 242.0 ns (-2%) 904 B → 904 B 0.67× → 0.64× 1.70× → 1.71×
Property (N=10) 596.7 ns → 1.19 μs (+100%) 1,768 B → 1,768 B 0.59× → 0.26× 🔴 +127% 2.71× → 1.31× 🔴 +106%
Property (N=1000) 40.33 μs → 38.31 μs (-5%) 96,808 B → 96,808 B 0.49× → 0.42× 🔸 +16% 3.04× → 3.01×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.67GHz, 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  

Property N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 242.0 ns 4.38 ns 3.65 ns 1.00 904 B 1.00
Imposter 1 156.1 ns 0.68 ns 0.53 ns 0.65 672 B 0.74
TUnitMocks 1 412.7 ns 2.90 ns 2.42 ns 1.71 1672 B 1.85
Moq 1 11,047.7 ns 164.50 ns 145.83 ns 45.66 10569 B 11.69
NSubstitute 1 7,631.9 ns 31.52 ns 29.48 ns 31.54 11664 B 12.90
FakeItEasy 1 7,799.1 ns 46.36 ns 41.10 ns 32.23 11196 B 12.38
Mockolate 10 1,191.5 ns 12.29 ns 10.27 ns 1.00 1768 B 1.00
Imposter 10 308.3 ns 3.55 ns 3.32 ns 0.26 960 B 0.54
TUnitMocks 10 1,564.3 ns 9.42 ns 8.81 ns 1.31 4024 B 2.28
Moq 10 17,672.7 ns 183.84 ns 153.51 ns 14.83 18505 B 10.47
NSubstitute 10 17,110.5 ns 79.73 ns 74.58 ns 14.36 21529 B 12.18
FakeItEasy 10 19,066.9 ns 121.25 ns 113.42 ns 16.00 30785 B 17.41
Mockolate 1000 38,312.4 ns 192.75 ns 170.87 ns 1.00 96808 B 1.00
Imposter 1000 16,210.0 ns 161.27 ns 150.85 ns 0.42 32641 B 0.34
TUnitMocks 1000 115,299.4 ns 953.81 ns 892.19 ns 3.01 242489 B 2.50
Moq 1000 718,327.2 ns 5,594.59 ns 5,233.19 ns 18.75 836541 B 8.64
NSubstitute 1000 1,052,778.2 ns 6,147.57 ns 5,449.66 ns 27.48 1139703 B 11.77
FakeItEasy 1000 1,271,048.6 ns 12,810.34 ns 11,982.80 ns 33.18 2194290 B 22.67

CompleteUnconfiguredBenchmarks

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

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
Unconfigured (N=1) 396.8 ns → 334.1 ns (-16%) 1,512 B → 1,512 B 0.58× → 0.64× 1.22× → 1.24×
Unconfigured (N=10) 1.45 μs → 1.35 μs (-7%) 3,984 B → 3,984 B 0.58× → 0.60× 1.98× → 1.89×
Unconfigured (N=1000) 115.49 μs → 109.36 μs (-5%) 273,265 B → 273,265 B 0.58× → 0.62× 2.19× → 2.22×
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 334.1 ns 4.20 ns 3.73 ns 1.00 1.48 KB 1.00
Imposter 1 214.5 ns 1.85 ns 1.55 ns 0.64 1.06 KB 0.72
TUnitMocks 1 413.9 ns 4.84 ns 4.52 ns 1.24 1.81 KB 1.23
Moq 1 70,287.8 ns 392.00 ns 347.50 ns 210.41 9.79 KB 6.63
NSubstitute 1 5,466.3 ns 89.44 ns 79.29 ns 16.36 10.17 KB 6.89
FakeItEasy 1 5,156.0 ns 89.13 ns 79.01 ns 15.43 11.69 KB 7.91
Mockolate 10 1,345.9 ns 23.29 ns 21.79 ns 1.00 3.89 KB 1.00
Imposter 10 808.2 ns 11.57 ns 10.26 ns 0.60 2.89 KB 0.74
TUnitMocks 10 2,539.6 ns 70.12 ns 65.59 ns 1.89 8.47 KB 2.18
Moq 10 81,954.9 ns 302.27 ns 282.75 ns 60.91 24.53 KB 6.30
NSubstitute 10 17,623.4 ns 216.64 ns 202.64 ns 13.10 25.06 KB 6.44
FakeItEasy 10 20,076.2 ns 589.49 ns 551.41 ns 14.92 54.03 KB 13.89
Mockolate 1000 109,363.8 ns 418.15 ns 370.68 ns 1.00 266.86 KB 1.00
Imposter 1000 67,422.4 ns 588.85 ns 522.00 ns 0.62 203.99 KB 0.76
TUnitMocks 1000 243,203.0 ns 4,974.20 ns 4,652.87 ns 2.22 691.21 KB 2.59
Moq 1000 1,177,444.9 ns 4,529.40 ns 3,536.25 ns 10.77 1653 KB 6.19
NSubstitute 1000 1,411,499.2 ns 6,868.02 ns 6,088.32 ns 12.91 1653.22 KB 6.20
FakeItEasy 1000 2,307,157.5 ns 127,990.80 ns 119,722.67 ns 21.10 4691.08 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 1.14 μs → 1.26 μs (+11%) 2,872 B → 2,872 B 0.97× → 0.90× 2.97× → 2.76×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.79GHz, 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  

Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.261 μs 0.0458 μs 0.0406 μs 1.00 2.8 KB 1.00
Imposter 1.137 μs 0.0285 μs 0.0267 μs 0.90 3.17 KB 1.13
TUnitMocks 3.478 μs 0.1124 μs 0.1052 μs 2.76 10.3 KB 3.67
Moq 551.183 μs 5.5054 μs 5.1497 μs 437.47 46.28 KB 16.50
NSubstitute 25.117 μs 0.4318 μs 0.3828 μs 19.94 37.13 KB 13.24
FakeItEasy 26.239 μs 0.5204 μs 0.4868 μs 20.83 34.34 KB 12.24

MockCreationBenchmarks

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

All benchmarks compared to main
Benchmark Time Memory vs Imposter vs TUnitMocks
CreateMock 16.1 ns → 16.3 ns (+2%) 136 B → 136 B 5.90× → 5.82× 1.72× → 1.82×
Raw results
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.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-v3

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

CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 16.33 ns 0.670 ns 0.627 ns 1.00 136 B 1.00
Imposter 94.97 ns 2.268 ns 2.122 ns 5.82 504 B 3.71
TUnitMocks 29.68 ns 1.107 ns 0.982 ns 1.82 216 B 1.59
Moq 1,389.62 ns 12.328 ns 11.531 ns 85.21 2088 B 15.35
NSubstitute 1,927.31 ns 32.377 ns 30.285 ns 118.19 5032 B 37.00
FakeItEasy 1,589.90 ns 40.983 ns 38.336 ns 97.49 2755 B 20.26

@vbreuss
vbreuss force-pushed the fix/generic-interface-closed-over-dynamic branch from e1c7337 to 3751762 Compare November 18, 2026 16:50
@vbreuss
vbreuss enabled auto-merge (squash) November 18, 2026 16:59
@vbreuss
vbreuss disabled auto-merge November 18, 2026 18:11
@vbreuss
vbreuss enabled auto-merge (squash) November 18, 2026 19:24
Mocking a generic type that is closed over `dynamic`, such as `IRepo<dynamic>.CreateMock()`, generated code that did not compile in classic and in union mode: a type constructed with `dynamic` can neither be implemented nor be a constraint (CS1966, CS1968), and the mock named the mocked type in both positions (the mock class, `IMockSetup<T>`, `IMockVerify<T>`, the constraints of `As<T>` and `Implementing<T>`). This also affected class and delegate mocks (`Base<dynamic>`, `Factory<dynamic>`), nested arguments (`IRepo<List<dynamic>>`) and `.Implementing<IRepo<dynamic>>()`. #982 only covered members typed as `dynamic`.

`IRepo<dynamic>` and `IRepo<object>` are identity-convertible, so the generator now mocks the identical type that is constructed with `object`: before it reads a mocked type (the type `CreateMock` is called on and every `Implementing<T>` type), it replaces `dynamic` with `object` at any depth of the type symbol (type arguments, arrays, tuples and containing types). The mock of `IRepo<dynamic>` therefore is the mock of `IRepo<object>` (`Mock.IRepo_object`, "IRepo<object> mock"), which is assignable to `IRepo<dynamic>`, and a member type that comes from the type argument is emitted with `object` as well (`List<T>` as `List<object>`).

The symbol is replaced instead of the emitted names, so that both spellings are the same mock by construction: they produce equal entities, the existing deduplication of the mocks and of the `As<T>` extensions applies unchanged, and the generated sources do not depend on which spelling is used or which one comes first. With only the names replaced, the entities of the two spellings would still differ in their member types and documentation references, and combination mocks that use both spellings would declare the `Implementing` and `As<T>` extensions twice (CS0121, CS0111), also across different combinations such as `IOther.CreateMock().Implementing<IRepo<dynamic>>()` and `IRepo<object>.CreateMock().Implementing<IOther>()`.

Only the mocked types are replaced. A member that is declared with `dynamic` is emitted as before, also when it is inherited from a base class that a declaration constructs with `dynamic` (`class Derived : Base<dynamic>`), so the mocks that compiled before are generated unchanged.

Example:

```csharp
public interface IRepo<T>
{
    T Get(T key);
    void Add(T item);
}

IRepo<dynamic> sut = IRepo<dynamic>.CreateMock();
sut.Mock.Setup.Get("key").Returns("value");

object value = sut.Get("key");
sut.Add(42);

sut.Mock.Verify.Add(42).Once();
```
@vbreuss
vbreuss force-pushed the fix/generic-interface-closed-over-dynamic branch from 3751762 to 7f18f76 Compare November 18, 2026 19:32
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss merged commit c1503ac into main Nov 18, 2026
24 checks passed
@vbreuss
vbreuss deleted the fix/generic-interface-closed-over-dynamic branch November 18, 2026 19:44
github-actions Bot added a commit that referenced this pull request Nov 18, 2026
github-actions Bot added a commit that referenced this pull request Nov 18, 2026
@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