Stathijack 1.1.0
dotnet add package Stathijack --version 1.1.0
NuGet\Install-Package Stathijack -Version 1.1.0
<PackageReference Include="Stathijack" Version="1.1.0" />
<PackageVersion Include="Stathijack" Version="1.1.0" />
<PackageReference Include="Stathijack" />
paket add Stathijack --version 1.1.0
#r "nuget: Stathijack, 1.1.0"
#:package Stathijack@1.1.0
#addin nuget:?package=Stathijack&version=1.1.0
#tool nuget:?package=Stathijack&version=1.1.0
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 | 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 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. |
-
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.