stub0 1.0.16
dotnet add package stub0 --version 1.0.16
NuGet\Install-Package stub0 -Version 1.0.16
<PackageReference Include="stub0" Version="1.0.16" />
<PackageVersion Include="stub0" Version="1.0.16" />
<PackageReference Include="stub0" />
paket add stub0 --version 1.0.16
#r "nuget: stub0, 1.0.16"
#:package stub0@1.0.16
#addin nuget:?package=stub0&version=1.0.16
#tool nuget:?package=stub0&version=1.0.16
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
usingscope 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, andConstructorContext: 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(...), andOnEventRemove(...)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>()whenTis a class or abstract classStub0.CreatePatchingScope(instance)for non-interface instancesStub0.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 | Versions 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. |
-
net10.0
- Iced (>= 1.21.0)
- JetBrains.Annotations (>= 2025.2.4)
-
net8.0
- Iced (>= 1.21.0)
- JetBrains.Annotations (>= 2025.2.4)
-
net9.0
- Iced (>= 1.21.0)
- JetBrains.Annotations (>= 2025.2.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.