Mockolate 0.21.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mockolate --version 0.21.0
                    
NuGet\Install-Package Mockolate -Version 0.21.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="Mockolate" Version="0.21.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mockolate" Version="0.21.0" />
                    
Directory.Packages.props
<PackageReference Include="Mockolate" />
                    
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 Mockolate --version 0.21.0
                    
#r "nuget: Mockolate, 0.21.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 Mockolate@0.21.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=Mockolate&version=0.21.0
                    
Install as a Cake Addin
#tool nuget:?package=Mockolate&version=0.21.0
                    
Install as a Cake Tool

Mockolate

Changelog Nuget Coverage Mutation testing badge

Mockolate logo

Mockolate is a modern, strongly-typed mocking library for .NET, powered by source generators. It enables fast, compile-time validated mocks for interfaces and classes, supporting .NET Standard 2.0, .NET 8, .NET 10, and .NET Framework 4.8.

  • Source generator-based: No runtime proxy generation, fast and reliable.
  • Strongly-typed: Setup and verify mocks with full IntelliSense and compile-time safety.
  • AOT compatible: Works with NativeAOT and trimming.

Getting Started

  1. Install the Mockolate nuget package

    dotnet add package Mockolate
    
  2. Create a mock

    using Mockolate;
    
    var mock = Mock.Create<IMyInterface>();
    

Features

Mock Creation

  • Create mocks for interfaces and classes:
    var mock = Mock.Create<IMyInterface>();
    var classMock = Mock.Create<MyVirtualClass>();
    
  • Provide a MockBehavior to control the default behavior of the mock.
  • Use a Mock.Factory to pass a common behavior to all created mocks.

Setup / Arrange

Set up return values or behaviors for methods and properties on your mock. Control how the mock responds to calls in your tests.

Method setup
mock.Setup.AddUser(With.Any<string>())
    .Returns(name => new User(Guid.NewGuid(), name));
  • Use .Callback(…) to run code when the method is called.
  • Use .Returns(…) to specify the value to return. You can provide a direct value or a callback to generate values on demand.
  • Use .Throws(…) to specify an exception to throw when the method is executed.
  • Use .Returns(…) and .Throws(…) repeatedly to define a sequence of return values.

Argument Matching

Mockolate provides flexible argument matching for method setups and verifications:

  • With.Any<T>(): Matches any value of type T.
  • With.Matching<T>(predicate): Matches values based on a predicate.
  • With.Ref<T>(…)/With.Out<T>(…): Matches and sets ref or out parameters.
mock.Setup.AddUser(With.Matching<string>(name => name.StartsWith("A")))
    .Returns(new User(Guid.NewGuid(), "Alicia"));

mock.Setup.TryDelete(With.Any<Guid>(), With.Out<User?>(() => new User(id, "Alice")))
    .Returns(true);
Property Setup

Set up property getters and setters to control or verify property access on your mocks. Supports auto-properties and indexers.

Initialization
You can initialize properties and they will work like normal properties (setter changes the value, getter returns the last set value).

mock.Setup.Property.MyProperty.InitializeWith(42);

Returns / Throws
Alternatively you can set up the properties similar to methods with Returns and Throws.

mock.Setup.Property.MyProperty
	.Returns(1)
	.Returns(2)
	.Throws(new Exception("Error"))
	.Returns(4);

Callbacks
Callbacks can be registered on the setter or getter.

mock.Setup.Property.MyProperty.OnGet(() => Console.WriteLine("MyProperty was read!"));
mock.Setup.Property.MyProperty.OnSet(value => Console.WriteLine($"Set MyProperty to {value}!"));

Indexers
Indexers are supported as well.

mock.Setup.Indexer(With.Any<int>())
	.InitializeWith(index => index*index)
	.OnGet(index => Console.WriteLine($"Indexer this[{index}] was read"));

Event Raising

Easily raise events on your mock to test event handlers in your code:

mock.Raises.UsersChanged(this, EventArgs.Empty);
  • Use the Raises property to trigger events declared on the mocked interface or class.
  • Simulate notifications and test event-driven logic.

Verification

Verify that methods or properties were called with specific arguments and how many times:

mock.Verify.Invoked.AddUser("Bob").AtLeastOnce();
mock.Verify.Invoked.TryDelete(id, With.Out<User?>()).Never();
mock.Verify.Invoked.DoSomething(With.Any<int>()).Exactly(2);
  • Supports .Never(), Once(), Twice(), Exactly(n), .AtLeastOnce(), .AtLeastTwice(), .AtLeast(n), .AtMostOnce(), .AtMostTwice(), .AtMost(n) for call count verification.
  • Verify arguments with matchers.
Call Ordering

Use Then to verify that calls occurred in a specific order:

mock.Verify.Invoked.AddUser("Alice").Then(
    m => m.Invoked.DeleteUser("Alice")
);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Mockolate:

Package Downloads
aweXpect.Mockolate

Expectations to verify interactions with mocks from Mockolate.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 92 1/25/2026
1.0.2 49 1/24/2026
1.0.1 50 1/24/2026
1.0.0 259 1/19/2026
0.53.0 201 1/18/2026
0.52.0 82 1/17/2026
0.51.0 86 1/12/2026
0.50.0 93 1/9/2026
0.49.0 93 1/6/2026
0.48.0 98 1/2/2026
0.47.0 94 1/2/2026
0.46.0 178 12/23/2025
0.45.0 176 12/13/2025
0.44.0 115 12/13/2025
0.43.0 414 12/11/2025
0.42.0 422 12/8/2025
0.41.0 311 12/5/2025
0.40.0 259 12/4/2025
0.39.0 189 12/4/2025
0.38.1 631 12/1/2025
0.38.0 257 11/30/2025
0.37.0 181 11/26/2025
0.36.0 333 11/24/2025
0.35.1 207 11/24/2025
0.35.0 200 11/24/2025
0.34.0 159 11/23/2025
0.33.0 160 11/22/2025
0.32.1 396 11/20/2025
0.32.0 389 11/20/2025
0.31.2 392 11/19/2025
0.31.1 389 11/19/2025
0.31.0 392 11/19/2025
0.30.0 224 11/16/2025
0.28.0 135 11/8/2025
0.27.0 188 11/5/2025
0.26.0 189 11/4/2025
0.25.0 195 11/2/2025
0.24.0 124 11/1/2025
0.23.0 119 11/1/2025
0.22.0 164 10/26/2025
0.21.0 165 10/26/2025
0.20.0 144 10/26/2025
0.19.0 140 10/25/2025
0.18.0 104 10/25/2025
0.17.0 101 10/25/2025
0.16.0 104 10/25/2025
0.15.0 102 10/25/2025
0.14.0 116 10/24/2025
0.13.0 126 10/24/2025
0.12.0 175 10/20/2025
0.11.0 113 10/18/2025
0.10.2 172 10/15/2025
0.10.1 172 10/13/2025
0.10.0 460 10/12/2025
0.9.1 220 10/12/2025
0.9.0 173 10/12/2025
0.8.0 112 10/11/2025
0.7.0 176 10/10/2025
0.6.0 167 10/8/2025
0.5.4 372 10/8/2025
0.5.3 171 10/8/2025
0.5.2 166 10/7/2025
0.5.1 171 10/7/2025
0.5.0 170 10/7/2025
0.4.0 167 10/7/2025
0.3.0 165 10/5/2025
0.2.0 167 10/5/2025
0.1.0 106 10/4/2025