C# 是否可以使用NSubstitute模拟局部方法变量?

C# 是否可以使用NSubstitute模拟局部方法变量?,c#,unit-testing,testing,nsubstitute,C#,Unit Testing,Testing,Nsubstitute,我有一个带有过程方法的类,在这个方法中我设置了一些东西,例如 public class messageProcessor { ... public string Process(string settings) { var elementFactory = new ElementFactory(); var strategyToUse = new legacyStrategy(); ... var resources = new messageResour

我有一个带有过程方法的类,在这个方法中我设置了一些东西,例如

public class messageProcessor
{
...
  public string Process(string settings)
  {
    var elementFactory = new ElementFactory();
    var strategyToUse = new legacyStrategy();
    ...
    var resources = new messageResource(
       elementFactory,
       strategyToUse,
       ...);
  }
}
我是否可以创建这个类的实例,但是当我调用Process方法时,替换(例如)elementFactory以设置为我的mocked factory


这可能吗?我该怎么做?谢谢

如果你的代码依赖于
元素工厂
,你可以通过
消息处理器
类的构造函数注入这个类的接口

这就是所谓的

例如,您创建了一个接口
IElementFactory
,可以通过构造函数将其注入类中,如下所示:

public class messageProcessor
{
    private readonly IElementFactory elementFactory;

    public messageProcessor(IElementFactory elementFactory)
    {
        this.elementFactory = elementFactory;
    }

    public string Process(string settings)
    {
        var strategyToUse = new legacyStrategy();
        ...
        var resources = new messageResource(
           this.elementFactory,
           strategyToUse,
           ...);
    }
}
现在,在您的测试中,您可以注入一个替代的
IElementFactory
。像这样:

public void Test()
{
    var elementFactory = Substitute.For<IElementFactory>();

    // tell the substitute what it should return when a specific method is called.
    elementFactory.AnyMethod().Returns(something);

    var processor = new messageProcessor(elementFactory);
}
公共无效测试()
{
var elementFactory=Substitute.For();
//告诉替换在调用特定方法时应该返回什么。
elementFactory.AnyMethod().返回(某物);
var处理器=新消息处理器(elementFactory);
}

在运行时,应用程序应将
IElementFactory
的实例注入
messageProcessor
类。你应该通过互联网来做这件事

如果您的代码依赖于
ElementFactory
,则可以通过
MessageProcessor
类的构造函数注入此类的接口

这就是所谓的

例如,您创建了一个接口
IElementFactory
,可以通过构造函数将其注入类中,如下所示:

public class messageProcessor
{
    private readonly IElementFactory elementFactory;

    public messageProcessor(IElementFactory elementFactory)
    {
        this.elementFactory = elementFactory;
    }

    public string Process(string settings)
    {
        var strategyToUse = new legacyStrategy();
        ...
        var resources = new messageResource(
           this.elementFactory,
           strategyToUse,
           ...);
    }
}
现在,在您的测试中,您可以注入一个替代的
IElementFactory
。像这样:

public void Test()
{
    var elementFactory = Substitute.For<IElementFactory>();

    // tell the substitute what it should return when a specific method is called.
    elementFactory.AnyMethod().Returns(something);

    var processor = new messageProcessor(elementFactory);
}
公共无效测试()
{
var elementFactory=Substitute.For();
//告诉替换在调用特定方法时应该返回什么。
elementFactory.AnyMethod().返回(某物);
var处理器=新消息处理器(elementFactory);
}

在运行时,应用程序应将
IElementFactory
的实例注入
messageProcessor
类。你应该通过互联网来做这件事

该代码与实现问题紧密耦合,这使得隔离测试变得困难。考虑这些依赖项上的反转控制,并通过构造函数或方法注入显式注入这些代码。该代码与实现隔离的测试关注紧密耦合。考虑这些依赖项上的反转控制,并通过构造函数或方法注入明确地注入它们。谢谢L01NL,我希望解决一个解决方案,我不需要修改现有代码,但这证实了我的想法,不可能做到这一点。非常感谢。感谢L01NL,我希望有一个解决方案,我不需要修改现有的代码,但这证实了我的想法,这是不可能做到的。非常感谢。