NSubstitute Mock static class 和静态方法

NSubstitute Mock static class and static method

我是单元测试的新手,我正在尝试在静态 class 中模拟静态方法。我已经读到您不能这样做,但我正在寻找解决该问题的方法。

我不能修改代码,并且制作相同的功能而不是静态的不是一个选项,因为他们检查测试的代码覆盖率,我至少需要 90%。
我已经尝试模拟它使用的变量,但它不起作用。

public static class MyClass
{
    public static response MyMethod(HttpSessionStateBase Session, 
        otherVariable, stringVariable)
    {
        //some code
    }
}

public ActionResult MyClassTested()
{
    var response = MyClass.MyMethod(Session);
    //more code
}

我的问题是这个方法在一个控制器中,该控制器用响应声明一个 var,并根据它重定向用户。

如果您不能修改代码,那么我认为使用 DynamicProxy-based 库(如 NSubstitute)无法解决此问题。这些库使用 inheritance to intercept members on classes,这对于静态和 non-virtual 成员是不可能的。

我建议尝试 Fakes。该页面上的示例之一涵盖了存根 DateTime.Now

可以模拟静态成员的其他替代方案包括 TypeMock 和 Telerik JustMock。

相关问题:

对于这类问题可能有更好的解决方案...取决于您能逃脱什么。

我最近 运行 在编写了一个静态实用程序 class 之后,我自己也参与其中,本质上,它用于创建 Guid 格式的各种截断。在编写集成测试时,我意识到我需要控制从该实用程序 class 生成的 运行dom Id,以便我可以故意将此 Id 发布到 API 然后断言结果。

我当时采用的解决方案是从静态 class 提供实现,但从非静态 class 中调用该实现(包装静态方法调用),我可以注册并注入到 DI 容器中。这个非静态 class 将是主要的主力,但静态实现在我需要从另一个静态方法调用这些方法的实例中可用(例如,我已经编写了很多集成设置代码作为IWevApplicationFactory 上的扩展,并使用静态实用程序创建数据库名称)。

在代码中,例如

// my static implementation - only use this within other static methods when necessary. Avoid as much as possible.
public static class StaticGuidUtilities 
{
    public static string CreateShortenedGuid([Range(1, 4)] int take)
    {
        var useNumParts = (take > 4) ? 4 : take;
        var ids = Guid.NewGuid().ToString().Split('-').Take(useNumParts).ToList();
        return string.Join('-', ids);
    }
}


// Register this abstraction in the DI container and use it as the default guid utility class
public interface IGuidUtilities
{
    string CreateShortenedGuid([Range(1, 4)] int take);
}

// Non-static implementation
public class GuidUtitlities : IGuidUtilities
{
    public string CreateShortenedGuid([Range(1, 4)] int take)
    {
        return StaticGuidUtilities.CreateShortenedGuid(take);
    }
}

----

// In the tests, I can use NSubstitute...
// (Doesn't coding to the abstraction make our lives so much easier?)
var guidUtility = Substitute.For<IGuidUtilities>();
var myTestId = "test-123";
guidUtility.CreateShortenedGuid(1).Returns(myTestId);

// Execute code and assert on 'myTestId' 
// This method will call the injected non-static utilty class and return the id
var result = subjectUndertest.MethodUnderTest();

// Shouldly syntax
result.Id.ShouldBe(myTestId);