Mockolate 0.17.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mockolate --version 0.17.0
                    
NuGet\Install-Package Mockolate -Version 0.17.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.17.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mockolate" Version="0.17.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.17.0
                    
#r "nuget: Mockolate, 0.17.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.17.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.17.0
                    
Install as a Cake Addin
#tool nuget:?package=Mockolate&version=0.17.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
0.27.0 0 11/5/2025
0.26.0 40 11/4/2025
0.25.0 54 11/2/2025
0.24.0 48 11/1/2025
0.23.0 49 11/1/2025
0.22.0 148 10/26/2025
0.21.0 150 10/26/2025
0.20.0 128 10/26/2025
0.19.0 126 10/25/2025
0.18.0 90 10/25/2025
0.17.0 88 10/25/2025
0.16.0 91 10/25/2025
0.15.0 87 10/25/2025
0.14.0 101 10/24/2025
0.13.0 111 10/24/2025
0.12.0 165 10/20/2025
0.11.0 102 10/18/2025
0.10.2 160 10/15/2025
0.10.1 157 10/13/2025
0.10.0 319 10/12/2025
0.9.1 208 10/12/2025
0.9.0 163 10/12/2025
0.8.0 104 10/11/2025
0.7.0 167 10/10/2025
0.6.0 158 10/8/2025
0.5.4 361 10/8/2025
0.5.3 162 10/8/2025
0.5.2 157 10/7/2025
0.5.1 162 10/7/2025
0.5.0 160 10/7/2025
0.4.0 158 10/7/2025
0.3.0 156 10/5/2025
0.2.0 156 10/5/2025
0.1.0 96 10/4/2025