AutoMock.Generator
1.0.0
See the version list below for details.
dotnet add package AutoMock.Generator --version 1.0.0
NuGet\Install-Package AutoMock.Generator -Version 1.0.0
<PackageReference Include="AutoMock.Generator" Version="1.0.0" />
<PackageVersion Include="AutoMock.Generator" Version="1.0.0" />
<PackageReference Include="AutoMock.Generator" />
paket add AutoMock.Generator --version 1.0.0
#r "nuget: AutoMock.Generator, 1.0.0"
#:package AutoMock.Generator@1.0.0
#addin nuget:?package=AutoMock.Generator&version=1.0.0
#tool nuget:?package=AutoMock.Generator&version=1.0.0
AutoMock.Generator
Compile-time mocking for .NET using Roslyn source generators. Add [Mock(typeof(IFoo))] to a partial
class — AutoMock generates a plain, debuggable test double implementing IFoo at build time, with a
settable *_Handler delegate and a *_Calls argument log for every member. No reflection, no
Castle.Core dynamic proxies — a compile-time alternative to Moq / NSubstitute for interfaces, fully
AOT/trim-safe.
Why
Moq and NSubstitute build mocks at runtime via System.Reflection.Emit / Castle.DynamicProxy. That:
- Doesn't work (or needs extra configuration) under Native AOT / trimming.
- Adds startup cost building proxy types.
- Produces an implementation you can't set a breakpoint in or read.
AutoMock generates a normal, debuggable partial class per [Mock] usage, at build time. You can read
it, step into it, and it fails the build (not the test run) if the interface has a member AutoMock
can't yet generate.
Usage
using AutoMock;
public interface ICalculator
{
int Add(int a, int b);
Task<int> AddAsync(int a, int b);
void Reset();
string Name { get; set; }
}
[Mock(typeof(ICalculator))]
public partial class CalculatorMock
{
}
Generates a CalculatorMock implementing ICalculator:
var mock = new CalculatorMock
{
Add_Handler = (a, b) => a + b, // configure return behavior
};
mock.Add(2, 3); // 5
mock.Add_CallCount; // 1
mock.Add_Calls[0]; // object?[] { 2, 3 }
ICalculator asInterface = mock; // implements the interface directly
Every generated member gets:
| Suffix | Purpose |
|---|---|
{Member}_Handler |
Func<..>/Action<..> delegate you set to control the return value / side effect. If unset, methods return default/Task.CompletedTask/Task.FromResult(default). |
{Member}_Calls |
List<object?[]> — every invocation's arguments, in order. |
{Member}_CallCount |
Shorthand for {Member}_Calls.Count. |
Properties additionally get {Name}_GetCallCount / {Name}_SetCallCount counters; the getter/setter
themselves just round-trip a private backing field.
Requirements
- The target class must be
partial. - The
[Mock]constructor argument must be an interface type ([Mock(typeof(IFoo))]). - A class can implement multiple interfaces via multiple
[Mock]attributes.
Supported members (v1.0.0)
- Methods: synchronous,
void,Task, andTask<T>returning, with any number of by-value parameters. - Properties: get-only, set-only, and get/set.
- Inherited interface members (interfaces that extend other interfaces).
Not yet supported (v1.0.0 — see Roadmap)
Skipped with an AM003 info diagnostic (build still succeeds — implement these manually in the partial class if you need them):
- Generic methods.
ref/out/inparameters.- Indexers.
- Events.
Diagnostics
| ID | Severity | Meaning |
|---|---|---|
| AM001 | Error | [Mock] target class is not partial. |
| AM002 | Error | [Mock] argument is not an interface type. |
| AM003 | Info | Member skipped — unsupported shape (generic method / ref-out-in param / indexer / event). |
Roadmap
- Generic method support.
Verify-style assertion helpers (e.g.mock.Verify(x => x.Add_CallCount == 1)).ref/outparameter support.- Abstract class mocking (in addition to interfaces).
License
MIT
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.0: Initial release. [Mock(typeof(IFoo))] on a partial class generates a test double implementing the interface with per-member `*_Handler` delegates, `*_Calls` argument logs, and `*_CallCount`. Supports methods (sync, Task, Task<T>) and get/set properties. AM001 (target must be partial), AM002 (argument must be an interface type), AM003 (unsupported member: generic method, ref/out/in parameter, indexer, or event — skipped).