Skip to content

Zero-allocation, source-generated .NET mocking library. 34x faster than Moq with partial mocks, static mocking, fluent API, and async support.

License

Notifications You must be signed in to change notification settings

guinhx/NimbleMock

Repository files navigation

NimbleMock

Zero-allocation, source-generated C# mocking library for exceptional developer experience.

NuGet License: MIT GitHub

Why NimbleMock?

34x faster than Moq, 7x faster than NSubstitute in mock creation. 67% less memory allocation.

// Before (Moq) - 48,812ns, 10.37 KB
var mock = new Mock<IUserRepository>();
mock.Setup(x => x.GetById(1)).Returns(user);
mock.Setup(x => x.SaveAsync(It.IsAny<User>())).ReturnsAsync(true);
var repo = mock.Object;

// After (NimbleMock) - 1,415ns, 3.45 KB
var mock = Mock.Of<IUserRepository>()
    .Setup(x => x.GetById(1), user)
    .SetupAsync(x => x.SaveAsync(default!), true)
    .Build();

Key Features

  • Stack-allocated builders with arrays - zero GC pressure
  • Source generators create compile-time proxies (no Castle.DynamicProxy)
  • Fluent API with intelligent type inference
  • First-class async support (Task<T>, ValueTask<T>)
  • Partial mocks for large interfaces
  • Roslyn analyzers detect unverified calls at compile-time
  • No telemetry (looking at you, Moq)

Installation

dotnet add package NimbleMock

Quick Start

using NimbleMock;
using Xunit;

public interface IUserRepository
{
    User GetById(int id);
    Task<bool> SaveAsync(User user);
}

public class UserServiceTests
{
    [Fact]
    public void GetUser_ReturnsFromRepository()
    {
        // Arrange
        var expectedUser = new User { Id = 1, Name = "Alice" };
        var mock = Mock.Of<IUserRepository>()
            .Setup(x => x.GetById(1), expectedUser)
            .Build();

        var service = new UserService(mock.Object);

        // Act
        var result = service.GetUser(1);

        // Assert
        Assert.Equal(expectedUser, result);
        mock.Verify(x => x.GetById(1)).Once();
    }
}

Performance Comparison

Operation Moq NSubstitute NimbleMock
Setup 48,812 ns 9,937 ns 1,415 ns
Verification 1,795 ns 2,163 ns 585 ns
Memory (Setup) 10.37 KB 12.36 KB 3.45 KB
Memory (Verify) 2.12 KB 2.82 KB 0.53 KB

Benchmarks: .NET 8.0.22, x64, RyuJIT AVX2, Windows 11

Documentation

Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests.

License

MIT © 2025

About

Zero-allocation, source-generated .NET mocking library. 34x faster than Moq with partial mocks, static mocking, fluent API, and async support.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages