AutoMock.Generator 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoMock.Generator --version 1.1.0
                    
NuGet\Install-Package AutoMock.Generator -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoMock.Generator" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoMock.Generator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AutoMock.Generator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AutoMock.Generator --version 1.1.0
                    
#r "nuget: AutoMock.Generator, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AutoMock.Generator@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AutoMock.Generator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AutoMock.Generator&version=1.1.0
                    
Install as a Cake Tool

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

ref/out/in parameters

public interface IParser
{
    bool TryParse(string input, out int value);
}

[Mock(typeof(IParser))]
public partial class ParserMock { }
var mock = new ParserMock
{
    TryParse_Handler = (string input, out int value) =>
    {
        value = int.Parse(input);
        return true;
    },
};

mock.TryParse("42", out var result); // result == 42

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.1.0)

  • Methods: synchronous, void, Task, and Task<T> returning, with any number of ref/out/in or by-value parameters. Methods with ref/out/in parameters get a dedicated per-method delegate type ({Method}Delegate) since Func<>/Action<> can't express those signatures.
  • Properties: get-only, set-only, and get/set.
  • Inherited interface members (interfaces that extend other interfaces).
  • AutoMock_Reset() — clears every generated *_Handler, *_Calls log, and property backing field, so a single mock instance can be reused across tests (e.g. a shared xunit fixture).
  • {Member}_WasCalled — shorthand for {Member}_CallCount > 0.

Not yet supported (v1.1.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.
  • 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)).
  • Abstract class mocking (in addition to interfaces).

License

MIT

There are no supported framework assets in this package.

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.

Version Downloads Last Updated
1.3.0 129 9/20/2026
1.2.0 95 9/20/2026
1.1.0 92 9/20/2026
1.0.0 97 9/20/2026

1.1.0: ref/out/in parameter support (generates a dedicated per-method delegate type when Func/Action can't express the signature); *_WasCalled shorthand per method; AutoMock_Reset() clears every Handler/Calls/property backing field so a mock instance can be reused across tests. 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, indexer, or event — skipped).