Repository navigation
fix: mock generic types that are closed over dynamic - #1026
Conversation
Test Results 47 files ± 0 47 suites ±0 1h 7m 16s ⏱️ +28s 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.♻️ This comment has been updated with latest results. |
🚀 Benchmark ResultsCompared to CallbackBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompleteEventBenchmarksRelative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompleteIndexerBenchmarks✅ No changes in memory or relative performance (3 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompleteMethodBenchmarksRelative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompletePropertyBenchmarksRelative performance (how many times as long the other library takes as Mockolate, main → PR)
All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompleteUnconfiguredBenchmarks✅ No changes in memory or relative performance (3 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
CompleteWorkflowBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
MockCreationBenchmarks✅ No changes in memory or relative performance (1 benchmarks) All benchmarks compared to main
Raw resultsBenchmarkDotNet 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
|
e1c7337 to
3751762
Compare
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(); ```
3751762 to
7f18f76
Compare
|
… over dynamic (#1026) by Valentin Breuß
… over dynamic (#1026) by Valentin Breuß
|
This is addressed in release v4.2.0. |



Mocking a generic type that is closed over
dynamic, such asIRepo<dynamic>.CreateMock(), generated code that did not compile in classic and in union mode: a type constructed withdynamiccan 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 ofAs<T>andImplementing<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 asdynamic.IRepo<dynamic>andIRepo<object>are identity-convertible, so the generator now names the mocked type, its inherited types and the containing type of every member withobjectin place ofdynamicat any depth, by replacing thedynamicpart of the symbol display. The mock is therefore the mock ofIRepo<object>, which is assignable toIRepo<dynamic>, and it is also named afterobject(Mock.IRepo_object, "IRepo mock"). The types of the members are emitted as before.Because both spellings now produce the same
CreateMockandImplementingextensions, the collected mocks are deduplicated by their emitted type names instead of by equality. Otherwise mockingIRepo<dynamic>andIRepo<object>in one compilation would emit two mocks that differ only in how they spell nested member types (List<dynamic>andList<object>) and make the calls ambiguous (CS0121, CS0111).Example: