Skip to content

feat: add [AsDefault] to make an AutoMockArguments parameter the default value of its type - #1134

Merged
vbreuss merged 3 commits into
mainfrom
feat/as-default-parameters
Nov 25, 2026
Merged

vbreuss merged 3 commits into
mainfrom
feat/as-default-parameters

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

[AutoMockArguments] passes test parameters into the constructor of a class under test, but interface mocks have no constructor. When a mocked interface exposes another test parameter, every test has to set it up by hand:

[Test]
[AutoMockArguments]
public async Task Foo(IOrderService orders, IOrderRepository repository)
{
    orders.Mock.Setup.Repository.Returns(repository);
    // ...
}

With [AsDefault], the parameter becomes the default value of its exact type for all mocks of the test case:

[Test]
[AutoMockArguments]
public async Task Foo(IOrderService orders, [AsDefault] IOrderRepository repository)
{
    // orders.Repository returns `repository` without a setup
}

Behavior

  • It applies to properties, methods, indexers (keys that were never written) and out parameters, also wrapped in Task<T> or ValueTask<T>.
  • Setups, written values and ThrowWhenNotSetup still take precedence. For its type, it wins over the default values of AutoMockArgumentsAttribute.DefaultBehavior.
  • It matches the exact type only, like the constructor argument matching. The parameter is still passed to the constructor of a class under test.
  • Every member of the type returns the same instance, including factory-like methods and every indexer key. The docs mention this trade-off.

The marker registers a default value factory on the behavior of the test case through the existing WithDefaultValueFor, so the Mockolate library and its public API are unchanged. While the parameter itself is being created, the previous default values apply, so that its creation does not recurse.

Analyzer

The new rule Mockolate0011 (warning) reports an [AsDefault] parameter with the same type as another [AsDefault] parameter, and one that is also [Transient].

Docs

  • New section "Return a parameter from the other mocks" in the mock test parameters page, and the Mockolate0011 entry in the analyzers page.
  • The framework-specific sections of the mock test parameters page are now grouped under a common "Test frameworks" section.

…ult value of its type

Interface mocks have no constructor, so a test parameter they expose, e.g. IOrderService.Repository, had to be set up by hand in every test. [AsDefault] makes the parameter the default value of its type for all mocks of the test case, and Mockolate0011 reports markers that are ambiguous or contradict [Transient].
@vbreuss vbreuss self-assigned this Nov 25, 2026
@vbreuss vbreuss added the enhancement New feature or request label Nov 25, 2026
@github-actions

Copy link
Copy Markdown

Test Results

0 files   -    108  0 suites   - 108   0s ⏱️ - 1h 58m 13s
0 tests  -  5 866  0 ✅  -  5 864  0 💤  -  2  0 ❌ ±0 
0 runs   - 57 212  0 ✅  - 57 192  0 💤  - 20  0 ❌ ±0 

Results for commit 1a1722b. ± Comparison against base commit 4f46104.

@github-actions

github-actions Bot commented Nov 25, 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,720 B 105,721 B 🔴 +1 B (±0%)
Async (N=1000) 224,800 B 224,801 B 🔴 +1 B (±0%)

Time

Benchmark main PR Δ
CreateMock 18.8 ns 16.3 ns 🟢 -13%
Unconfigured (N=10) 1.86 μs 1.65 μs 🟢 -12%
Unconfigured (N=1000) 151.62 μs 130.75 μs 🟢 -14%
Property (N=10) 443.4 ns 396.6 ns 🟢 -11%
Indexer (N=1000) 60.59 μs 66.69 μs 🔴 +10%
CompleteWorkflowBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1.719 μs 0.0183 μs 0.0171 μs 1.00 5.14 KB 1.00
Imposter 1.934 μs 0.0607 μs 0.0568 μs 1.13 7.21 KB 1.40
TUnitMocks 3.273 μs 0.1470 μs 0.1375 μs 1.90 10.66 KB 2.07
Moq 799.780 μs 2.7188 μs 2.2703 μs 465.21 89.94 KB 17.50
NSubstitute 252.592 μs 0.8577 μs 0.7604 μs 146.93 53.08 KB 10.33
FakeItEasy 287.156 μs 0.8717 μs 0.7727 μs 167.03 64.05 KB 12.46
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  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 16.29 ns 1.115 ns 1.043 ns 1.00 136 B 1.00
Imposter 100.50 ns 4.125 ns 3.859 ns 6.19 504 B 3.71
TUnitMocks 32.45 ns 0.680 ns 0.602 ns 2.00 216 B 1.59
Moq 872.54 ns 7.534 ns 7.047 ns 53.75 2088 B 15.35
NSubstitute 1,282.21 ns 13.793 ns 12.902 ns 78.99 5032 B 37.00
FakeItEasy 1,103.39 ns 8.647 ns 7.666 ns 67.97 2751 B 20.23
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  

CompleteUnconfiguredBenchmarks
Unconfigured N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 473.4 ns 11.22 ns 8.76 ns 1.00 1.51 KB 1.00
Imposter 1 283.9 ns 2.07 ns 1.73 ns 0.60 1.06 KB 0.70
TUnitMocks 1 531.1 ns 6.19 ns 5.17 ns 1.12 1.74 KB 1.16
Moq 1 115,634.9 ns 456.14 ns 426.68 ns 244.36 9.79 KB 6.50
NSubstitute 1 8,236.4 ns 50.86 ns 42.47 ns 17.41 10.17 KB 6.75
FakeItEasy 1 7,798.4 ns 110.44 ns 103.30 ns 16.48 11.58 KB 7.68
Mockolate 10 1,647.3 ns 29.72 ns 27.80 ns 1.00 3.92 KB 1.00
Imposter 10 1,051.8 ns 20.90 ns 19.55 ns 0.64 2.89 KB 0.74
TUnitMocks 10 3,407.0 ns 31.92 ns 28.30 ns 2.07 8.4 KB 2.14
Moq 10 134,970.9 ns 1,169.98 ns 1,037.16 ns 81.96 24.53 KB 6.25
NSubstitute 10 26,460.5 ns 171.49 ns 152.02 ns 16.07 25.06 KB 6.39
FakeItEasy 10 29,697.7 ns 224.91 ns 199.37 ns 18.03 53.92 KB 13.75
Mockolate 1000 130,751.5 ns 1,332.98 ns 1,246.87 ns 1.00 266.89 KB 1.00
Imposter 1000 85,439.4 ns 879.99 ns 780.09 ns 0.65 203.99 KB 0.76
TUnitMocks 1000 320,415.6 ns 2,611.53 ns 2,315.05 ns 2.45 691.13 KB 2.59
Moq 1000 1,691,983.7 ns 21,891.45 ns 19,406.20 ns 12.94 1653.01 KB 6.19
NSubstitute 1000 2,030,982.8 ns 25,068.35 ns 23,448.95 ns 15.53 1653.23 KB 6.19
FakeItEasy 1000 2,912,898.2 ns 56,576.75 ns 50,153.81 ns 22.28 4690.81 KB 17.58
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  

CompleteMethodBenchmarks
Method N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 88.11 ns 2.255 ns 1.999 ns 1.00 592 B 1.00
Imposter 1 117.50 ns 5.297 ns 4.696 ns 1.33 808 B 1.36
TUnitMocks 1 231.09 ns 5.031 ns 4.706 ns 2.62 1424 B 2.41
Moq 1 2,661.11 ns 32.017 ns 24.997 ns 30.22 6552 B 11.07
NSubstitute 1 3,336.43 ns 117.952 ns 110.333 ns 37.89 9816 B 16.58
FakeItEasy 1 2,631.18 ns 83.835 ns 78.419 ns 29.88 7517 B 12.70
Mockolate 10 262.36 ns 5.499 ns 4.875 ns 1.00 1096 B 1.00
Imposter 10 292.71 ns 4.726 ns 4.190 ns 1.12 1168 B 1.07
TUnitMocks 10 937.56 ns 21.193 ns 19.824 ns 3.57 3288 B 3.00
Moq 10 4,026.28 ns 29.148 ns 25.839 ns 15.35 10536 B 9.61
NSubstitute 10 4,965.00 ns 34.377 ns 26.839 ns 18.93 12840 B 11.72
FakeItEasy 10 5,092.32 ns 84.961 ns 75.316 ns 19.42 15754 B 14.37
Mockolate 1000 18,022.04 ns 179.138 ns 167.566 ns 1.00 56536 B 1.00
Imposter 1000 14,718.95 ns 139.954 ns 109.267 ns 0.82 40768 B 0.72
TUnitMocks 1000 66,390.87 ns 840.859 ns 702.155 ns 3.68 178353 B 3.15
Moq 1000 123,746.24 ns 1,113.667 ns 987.237 ns 6.87 423563 B 7.49
NSubstitute 1000 161,056.88 ns 3,028.712 ns 2,529.111 ns 8.94 378535 B 6.70
FakeItEasy 1000 249,425.18 ns 7,940.949 ns 7,427.968 ns 13.84 943722 B 16.69
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.08GHz, 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 186.9 ns 3.34 ns 3.13 ns 1.00 920 B 1.00
Imposter 1 116.4 ns 0.72 ns 0.60 ns 0.62 672 B 0.73
TUnitMocks 1 241.5 ns 3.38 ns 3.16 ns 1.29 1464 B 1.59
Moq 1 5,338.2 ns 149.15 ns 139.52 ns 28.56 10784 B 11.72
NSubstitute 1 3,693.7 ns 111.26 ns 98.63 ns 19.76 11664 B 12.68
FakeItEasy 1 3,567.5 ns 89.63 ns 79.46 ns 19.09 11196 B 12.17
Mockolate 10 396.6 ns 7.82 ns 6.94 ns 1.00 1784 B 1.00
Imposter 10 254.4 ns 6.13 ns 5.44 ns 0.64 960 B 0.54
TUnitMocks 10 895.1 ns 19.05 ns 17.82 ns 2.26 3600 B 2.02
Moq 10 8,681.1 ns 176.28 ns 156.27 ns 21.89 18721 B 10.49
NSubstitute 10 8,670.1 ns 180.17 ns 168.53 ns 21.87 21529 B 12.07
FakeItEasy 10 8,923.3 ns 120.09 ns 106.46 ns 22.51 30785 B 17.26
Mockolate 1000 27,550.6 ns 424.04 ns 375.90 ns 1.00 96824 B 1.00
Imposter 1000 15,337.8 ns 421.57 ns 394.33 ns 0.56 32641 B 0.34
TUnitMocks 1000 71,657.3 ns 2,131.39 ns 1,889.42 ns 2.60 218305 B 2.25
Moq 1000 361,527.3 ns 7,126.53 ns 6,666.16 ns 13.12 836755 B 8.64
NSubstitute 1000 547,260.1 ns 14,880.17 ns 13,918.92 ns 19.87 1139700 B 11.77
FakeItEasy 1000 698,932.3 ns 33,931.24 ns 31,739.31 ns 25.37 2194324 B 22.66
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.34GHz, 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
Indexer N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 272.7 ns 11.71 ns 10.96 ns 1.00 1.78 KB 1.00
Imposter 1 200.1 ns 4.59 ns 4.29 ns 0.73 1.19 KB 0.67
TUnitMocks 1 359.4 ns 9.71 ns 9.08 ns 1.32 2.16 KB 1.21
Moq 1 145,575.1 ns 3,625.64 ns 3,027.57 ns 534.56 37.58 KB 21.10
NSubstitute 1 39,011.2 ns 1,381.77 ns 1,292.51 ns 143.25 27.45 KB 15.41
FakeItEasy 1 48,178.6 ns 1,327.38 ns 1,176.68 ns 176.91 29.57 KB 16.60
Mockolate 10 861.3 ns 20.39 ns 18.08 ns 1.00 2.7 KB 1.00
Imposter 10 455.6 ns 11.19 ns 10.47 ns 0.53 2.24 KB 0.83
TUnitMocks 10 1,330.3 ns 10.67 ns 9.46 ns 1.55 5.13 KB 1.90
Moq 10 154,204.5 ns 2,621.98 ns 2,452.60 ns 179.10 46.96 KB 17.42
NSubstitute 10 45,746.2 ns 1,659.64 ns 1,552.43 ns 53.13 40.25 KB 14.93
FakeItEasy 10 62,037.3 ns 2,414.42 ns 2,258.45 ns 72.05 48.75 KB 18.09
Mockolate 1000 66,685.2 ns 1,147.23 ns 1,016.99 ns 1.00 103.24 KB 1.00
Imposter 1000 27,395.0 ns 83.44 ns 73.97 ns 0.41 118.26 KB 1.15
TUnitMocks 1000 107,183.9 ns 2,382.73 ns 2,228.81 ns 1.61 292.52 KB 2.83
Moq 1000 574,119.5 ns 16,637.44 ns 15,562.67 ns 8.61 1023.74 KB 9.92
NSubstitute 1000 922,295.3 ns 31,445.08 ns 29,413.75 ns 13.83 1480.33 KB 14.34
FakeItEasy 1000 832,890.5 ns 26,083.93 ns 24,398.92 ns 12.49 2169.33 KB 21.01
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.51GHz, 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  

CompleteEventBenchmarks
Event Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 87.93 ns 4.083 ns 3.819 ns 1.00 712 B 1.00
Imposter 113.33 ns 2.434 ns 2.158 ns 1.29 512 B 0.72
TUnitMocks 102.20 ns 2.107 ns 1.868 ns 1.16 1392 B 1.96
Moq 7,774.92 ns 80.574 ns 71.427 ns 88.58 12760 B 17.92
NSubstitute 2,958.76 ns 81.812 ns 76.527 ns 33.71 9192 B 12.91
FakeItEasy 109,934.97 ns 2,882.123 ns 2,695.940 ns 1,252.47 15619 B 21.94
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.30GHz, 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 154.7 ns 3.67 ns 3.43 ns 1.00 800 B 1.00
Imposter 161.1 ns 3.65 ns 3.24 ns 1.04 616 B 0.77
TUnitMocks 270.1 ns 4.37 ns 3.88 ns 1.75 1352 B 1.69
Moq 61,720.7 ns 284.00 ns 237.16 ns 399.10 9056 B 11.32
NSubstitute 3,605.8 ns 79.56 ns 74.42 ns 23.32 7864 B 9.83
FakeItEasy 3,429.9 ns 113.68 ns 106.34 ns 22.18 6930 B 8.66
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  

AsyncBenchmarks
Async N Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 1 200.0 ns 7.30 ns 6.83 ns 1.00 1 KB 1.00
Imposter 1 265.0 ns 5.23 ns 4.64 ns 1.33 1.58 KB 1.58
TUnitMocks 1 476.4 ns 10.48 ns 8.75 ns 2.38 2.57 KB 2.57
Moq 1 90,269.5 ns 1,471.85 ns 1,376.77 ns 451.75 22.77 KB 22.77
NSubstitute 1 7,355.8 ns 209.33 ns 185.57 ns 36.81 19.09 KB 19.09
FakeItEasy 1 5,265.3 ns 105.26 ns 82.18 ns 26.35 14 KB 14.00
Mockolate 10 897.3 ns 14.32 ns 13.40 ns 1.00 2.97 KB 1.00
Imposter 10 855.0 ns 15.20 ns 14.22 ns 0.95 3.41 KB 1.15
TUnitMocks 10 2,594.2 ns 60.18 ns 53.35 ns 2.89 9.22 KB 3.11
Moq 10 110,406.4 ns 1,421.76 ns 1,187.24 ns 123.07 40.19 KB 13.54
NSubstitute 10 13,218.2 ns 450.26 ns 421.18 ns 14.73 29.39 KB 9.90
FakeItEasy 10 22,089.5 ns 587.65 ns 549.69 ns 24.62 59.08 KB 19.90
Mockolate 1000 82,001.0 ns 1,458.55 ns 1,292.96 ns 1.00 219.53 KB 1.00
Imposter 1000 71,949.8 ns 429.44 ns 401.70 ns 0.88 204.5 KB 0.93
TUnitMocks 1000 232,234.5 ns 6,991.23 ns 6,539.60 ns 2.83 638.22 KB 2.91
Moq 1000 2,103,447.8 ns 23,964.12 ns 22,416.06 ns 25.66 1891.02 KB 8.61
NSubstitute 1000 652,337.6 ns 22,408.15 ns 19,864.24 ns 7.96 1092.82 KB 4.98
FakeItEasy 1000 2,344,999.5 ns 45,809.98 ns 42,850.68 ns 28.60 4933.39 KB 22.47
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.18GHz, 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  

SequenceBenchmarks
Sequence Mean Error StdDev Ratio Allocated Alloc Ratio
Mockolate 3.338 μs 0.0267 μs 0.0237 μs 1.00 2.23 KB 1.00
Imposter 3.053 μs 0.0212 μs 0.0198 μs 0.91 2.23 KB 1.00
TUnitMocks 3.604 μs 0.0191 μs 0.0179 μs 1.08 2.84 KB 1.27
Moq 15.048 μs 0.0703 μs 0.0658 μs 4.51 11.12 KB 4.98
NSubstitute 12.817 μs 0.0625 μs 0.0522 μs 3.84 14.29 KB 6.40
FakeItEasy 13.580 μs 0.1797 μs 0.1681 μs 4.07 14.28 KB 6.39
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  

The AOT test app calls the AutoMockRow constructor directly, so it did not compile after the constructor got the AsDefault marker check.
SonarCloud reports the nested ternary operation as hard to read.
@vbreuss
vbreuss merged commit 1a94514 into main Nov 25, 2026
22 checks passed
@vbreuss
vbreuss deleted the feat/as-default-parameters branch November 25, 2026 20:26
@sonarqubecloud

Copy link
Copy Markdown

github-actions Bot added a commit that referenced this pull request Nov 25, 2026
…ckArguments parameter the default value of its type (#1134) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 25, 2026
…ckArguments parameter the default value of its type (#1134) by Valentin Breuß
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant