stub0 1.0.16

dotnet add package stub0 --version 1.0.16
                    
NuGet\Install-Package stub0 -Version 1.0.16
                    
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="stub0" Version="1.0.16" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="stub0" Version="1.0.16" />
                    
Directory.Packages.props
<PackageReference Include="stub0" />
                    
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 stub0 --version 1.0.16
                    
#r "nuget: stub0, 1.0.16"
                    
#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 stub0@1.0.16
                    
#: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=stub0&version=1.0.16
                    
Install as a Cake Addin
#tool nuget:?package=stub0&version=1.0.16
                    
Install as a Cake Tool

stub0

stub0 is a deterministic .NET mocking library for unit and integration tests. It starts with ordinary interface mocks and extends to runtime patching when you need to override concrete classes, one real object, static members, or constructors.

The goal is to keep those cases in one small workflow instead of making you switch between a mocking library for the easy path and a separate patching tool for the hard path.

Why stub0

  • Start with simple interface mocks and keep the same API when tests get harder.
  • Patch one existing object or one type inside a using scope with explicit ownership and cleanup.
  • Verify calls, order, and no-other-calls without leaving the same tool.
  • Fail fast with actionable diagnostics when runtime patching is required but the test host is not configured for it.

Pick the lightest API

  • Stub0.CreatePatched<IMyInterface>(): isolated interface mock with no runtime patching.
  • Stub0.CreatePatched<MyClass>(): isolated patched object for a class or abstract class.
  • Stub0.CreatePatchingScope(instance): temporary override for one existing object you already have.
  • Stub0.CreatePatchingScope<MyType>(): temporary override for static members and constructor hooks.
  • Arg, Times, CallContext, and ConstructorContext: argument matching, verification, and advanced behavior.

Install

dotnet add package stub0
<ItemGroup>
  <PackageReference Include="stub0" Version="x.y.z" />
</ItemGroup>

For supported .NET test frameworks (xUnit 2, xUnit 3, NUnit, MSTest, TUnit), the normal path is:

install stub0 -> run tests -> ready to go

stub0 ships build-transitive props, targets, and runsettings. RunSettingsFilePath is applied automatically only when the project is a test project and no custom runsettings path is already set.

First example

Start with CreatePatched<T>() when you want an isolated dependency, then configure behavior with On(...) and verify calls with Times.

using stub0;
using Xunit;

public interface IClock
{
    int Hour();
}

public sealed class GreetingService(IClock clock)
{
    public string GetGreeting() => clock.Hour() < 12 ? "Good morning" : "Good afternoon";
}

public class GreetingServiceTests
{
    [Fact]
    public void CreatePatched_builds_an_isolated_dependency()
    {
        using var clock = Stub0.CreatePatched<IClock>()
            .On(x => x.Hour()).Do(9);

        var sut = new GreetingService(clock.Result);

        Assert.Equal("Good morning", sut.GetGreeting());
        clock.Verify(x => x.Hour(), Times.Once);
    }
}

From there, the same mental model scales up:

  • Use CreatePatchingScope(instance) when the system already owns the real object.
  • Use CreatePatchingScope<MyType>() when the code you need to control is static or constructor-driven.
  • Use Sequence(...), OnSetter(...), OnIndexer(...), OnEventAdd(...), and OnEventRemove(...) for more specialized member shapes.

When runtime patching matters

Interface-only mocking via Stub0.CreatePatched<IMyInterface>() does not require runtime patching.

Runtime patching is required for:

  • Stub0.CreatePatched<T>() when T is a class or abstract class
  • Stub0.CreatePatchingScope(instance) for non-interface instances
  • Stub0.CreatePatchingScope<T>() for static members and constructor hooks

When runtime patching is needed, the test process must run with:

  • JIT inlining disabled
  • tiered compilation disabled
  • ReadyToRun disabled
  • dynamic code enabled

stub0 validates those prerequisites and throws with detailed guidance when they are not met.

Docs

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.16 86 3/17/2026
1.0.14 83 3/9/2026
1.0.13 80 3/4/2026
1.0.12 82 3/3/2026
1.0.11 90 2/25/2026
1.0.10 100 2/18/2026
1.0.9 105 1/19/2026