Getting started
Requirements
Mockolate generates its mocks with C# 14 extension members, so you need the .NET 10 SDK to build your tests. Your test projects can still target any supported framework: .NET Standard 2.0, .NET 8, .NET 10 or .NET Framework 4.8.
If your test project pins <LangVersion>, it must be 14 or later (or latest).
Install the package
- .NET CLI
- PackageReference
dotnet add package Mockolate
<PackageReference Include="Mockolate" Version="*" />
Replace * with the latest version.
Mockolate works with every test framework (xUnit, NUnit, MSTest, TUnit, …). The package contains the source generator and the analyzers, so there is nothing else to configure.
Write your first test
Suppose your code depends on a chocolate dispenser:
public interface IChocolateDispenser
{
bool Dispense(string type, int amount);
}
public class VendingMachine(IChocolateDispenser dispenser)
{
public string Buy(string type, int amount)
=> dispenser.Dispense(type, amount) ? "Enjoy!" : "Sold out";
}
To test VendingMachine without a real dispenser, create a mock of IChocolateDispenser, tell it how to behave, and
check afterwards how it was used:
using Mockolate;
using Mockolate.Verify;
// Arrange: create the mock and set up its behavior
IChocolateDispenser dispenser = IChocolateDispenser.CreateMock();
dispenser.Mock.Setup.Dispense("Dark", It.IsAny<int>()).Returns(true);
VendingMachine sut = new VendingMachine(dispenser);
// Act
string result = sut.Buy("Dark", 4);
// Assert: result is "Enjoy!", and the dispenser was called exactly once
dispenser.Mock.Verify.Dispense("Dark", 4).Once();
CreateMock()is generated for every type you mock, so IntelliSense shows it as soon as you typeIChocolateDispenser..- Every mock has a
Mockproperty that gives access toSetup,VerifyandRaise. It.IsAny<int>()is a parameter matcher. Plain values like"Dark"match by equality..Once()throws aMockVerificationExceptionwhen the call count does not match. It lives in theMockolate.Verifynamespace.
If you use aweXpect, the
aweXpect.Mockolate extension lets you write
verifications as awaitable expectations: await That(dispenser.Mock.Verify.Dispense("Dark", 4)).Once();.
Next steps
- Core concepts explains how mocks, setups and verifications fit together.
- A complete example combines properties, indexers, events and methods in one test.
- The cheat sheet lists the most common calls on one page.