Stathijack 1.1.0

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

Stathijack

Introduction

Stathijack is a tool that allows developers to either mock or replace static classes in unit or integration tests, without any changes to your production code whatsoever. You can easily replace the logic of static method to a simple Func, allowing you to use static classes in your code without fearing how that's going to be tested. This is possible by "hijacking" the original method, so whenever your code tries to access the static class, it is redirected to the specified mock or fake class.

Usage

Mocking a static method

using var mockingHijacker = new MockingHijacker(typeof(StaticClassUnderTest));
mockingHijacker.MockMethod("MethodName", (string nameOfTheParameter) =>
{
  //Some logic
});

Inspecting the invocations

using var mockingHijacker = new MockingHijacker(typeof(StaticClassUnderTest));
var mockedMethodData = mockingHijacker.MockMethod(...);

// Method calling logic

Assert.That(mockedMethodData.Invocations.Count, Is.EqualTo(1));

For more information and usages, check the Samples project and this article: https://intodot.net/mocking-static-classes-in-net-introducing-stathijack/

Contributing

If you are in need of new functionalities, feel free to raise an issue. I'd also gladly accept help regarding the issues mentioned in the 'Known issues' section.

Known issues

There are currently two main issues in this repository:

Unable to restore the original behavior

Once you have mocked the static class, the original behavior is lost. There is an experimental feature (HijackRegister.EnableExperimentalDefaultInvoking) which partially supports it, but it is far from being usable.

Unable to mock a method if it has been called already

If you have executed the method before adding the mock, it won't be possible to set up any mocks until the end of the test run.

There is also a weird behavior that is better shown than explained. Take the following situation:

[Test]
public void Test1()
{
    using var hijacker = new HijackRegister();
    var mockingHijacker = new MockingHijacker(typeof(Factory), hijacker);
    mockingHijacker.MockMethod(nameof(Factory.Create), (string _) => { return "Fake result"; });
    var test = new FactoryConsumer().Consume("Real result");
    var test2 = Factory.Create("Real result");
}

public class FactoryConsumer
{
    public string Consume(string teste)
    {
        return Factory.Create(teste);
    }
}

public static class Factory
{
    public static string Create(string teste)
    {
        return teste;
    }
}

When you execute this test, both "test" and "test2" variables will have "Fake result" as their value. However, if you swap their order to something like this:

var test = new FactoryConsumer().Consume("Real result");
var test2 = Factory.Create("Real result");

the mocking won't be applied at all, and their value will be "Real result".

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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.1.0 392 6/20/2024
1.0.0 137 6/8/2024