Snerte.bunit.AutoMocker 0.0.4

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

This project combines bunit and Moq.AutoMock making it super easy to create tests for Blazor Components. All dependencies are mocked automatically with AutoMocker so that you only have to setup the dependencies you use! Making test code super clean, easy to read and write.

Sample

Given Routes.razor.cs

public partial class Routes
{
    [Inject]
    IMydependency MyDependency { get; set; } = null!;

    [Inject]
    IMyOtherDependency MyOtherDependency { get; set; } = null!;

    protected override void OnInitialized()
    {
        MyDependency.GetSomeValue();
    }
}

Problem

Without AutoMocker the test setup process is tedious and requires a lot of maintenance.

public class WithoutAutoMockerTests : TestContext
{
    [Fact]
    public void MyTest()
    {
        // Arrange
        var myDependency = new Moq.Mock<IMydependency>();
        myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World");

        Services.AddSingleton<IMydependency>(myDependency.Object);

        var myOtherDependency = new Moq.Mock<IMyOtherDependency>();
        Services.AddSingleton<IMyOtherDependency>(myOtherDependency.Object);

        // Act
        var cut = RenderComponent<Routes>();

        // Assert
        Assert.Contains(cut.Markup, "Hello World");
    }
}

Every dependency needs to be setup, not setting up IMyOtherDepencency will cause the test to fail:

[Fact]
public void Fails_WhenAllOnlyUsedDependencyIsRegisterd()
{
    // Arrange
    var myDependency = new Moq.Mock<IMydependency>();
    myDependency.Setup(x => x.GetSomeValue()).Returns("Hello World");

    Services.AddSingleton<IMydependency>(myDependency.Object);

    // Act & Assert
    Assert.Throws<InvalidOperationException>(() => RenderComponent<Routes>());
}

Solution

With Automocker writing tests is simple. Change TestContext to AutoMockerTestContext and use the Mocker field from the baseclass to setup AutoMocker. For more info and AutoMocker recepies, check their docs

public class AutoMockerTestContextTests : AutoMockerTestContext
{
    [Fact]
    public void CreatesUnregisteredDependencies()
    {
        // Arrange
        Mocker.Use<IMydependency>(x => x.GetSomeValue() == "Hello World");

        // Act
        var cut = RenderComponent<Routes>();

        // Assert
        Assert.Contains(cut.Markup, "Hello World");
    }
}

When a certain type should not be handled by AutoMocker, a list of types can be provided via the constructor to exclude:

public class ExcludeTypesTests : AutoMockerTestContext
{
    public ExcludeTypesTests() : base(typeof(IMyOtherDependency))
    { }

    [Fact]
    public void WhenUnregisteredDependencyIsNotHandledByAutoMocker_ThrowsInvalidOperationException()
    {
        // Arrange
        Mocker.Use<IMydependency>(x => x.GetSomeValue() == "Hello World");

        // Act & Assert
        Assert.Throws<InvalidOperationException>(() => RenderComponent<Routes>());
    }
}
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 was computed.  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 was computed.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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
0.0.4 528 11/17/2025
0.0.3 616 9/9/2025
0.0.2 213 3/22/2024
0.0.1 182 3/22/2024