Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# StructureMap:除非在ObjectFactory中配置,否则使用常规参数_C#_Unit Testing_Mocking_Structuremap - Fatal编程技术网

C# StructureMap:除非在ObjectFactory中配置,否则使用常规参数

C# StructureMap:除非在ObjectFactory中配置,否则使用常规参数,c#,unit-testing,mocking,structuremap,C#,Unit Testing,Mocking,Structuremap,我觉得我正在努力完成一些不完全是“结构化地图方式”的事情,或者可能是,但我只是不知道如何去做。我希望有人能在这方面帮助我: 我正在编写一个插件,它需要有一个Execute方法,该方法在IServiceProvider中传递(由运行我的插件的应用程序提供)。 目前,我的代码如下所示: public void Execute(IServiceProvider serviceProvider) { //The serviceProvider is used to extract referen

我觉得我正在努力完成一些不完全是“结构化地图方式”的事情,或者可能是,但我只是不知道如何去做。我希望有人能在这方面帮助我:

我正在编写一个插件,它需要有一个
Execute
方法,该方法在
IServiceProvider
中传递(由运行我的插件的应用程序提供)。
目前,我的代码如下所示:

public void Execute(IServiceProvider serviceProvider)
{
    //The serviceProvider is used to extract references to other objects it supplies:
    this.Context = serviceProvider.GetService<IPluginExecutionContext>();

    //This could go more than one level deep:
    this.Acme = this.Context.Acme;

    //Do something with this.Context and this.Acme here...
}
public void Execute(IServiceProvider服务提供者)
{
//serviceProvider用于提取对其提供的其他对象的引用:
this.Context=serviceProvider.GetService();
//这可能会深入到多个层面:
this.Acme=this.Context.Acme;
//用这个做点什么。上下文和这个。这里是顶点。。。
}
这对于在生产环境中运行是很好的。但是,当我对这个插件进行单元测试时,我希望能够使用StructureMap插入模拟版本的IPluginExecutionContext或Acme

现在,我知道了如何让StructureMap为特定接口注册具体类型:

ObjectFactory.Initialize(x =>
{
    x.For<IPluginExecutionContext>()
     .Use<MockedPluginExecutionContext>();
});
ObjectFactory.Initialize(x=>
{
x、 For()
.使用();
});

但是,如果配置了此MockedPluginExecutionContext,如何让我的执行实现使用此MockedPluginExecutionContext,或者如果未配置,如何使用由
serviceProvider.GetService()
返回的值?

使用NSubstitute,我将执行以下操作,以单独测试
执行
(如果您希望这样做的话):

//排列
var objectUnderTest=。。。
var mockpluginexecontext=新的MockedPluginExecutionContext();
var mockProvider=Substitute.For();
mockProvider.GetService.Returns(mockpluginexcontext);
//表演
objectUnderTest.Execute(mockProvider);
//断言
断言。IsTrue(…);
// arrange
var objectUnderTest = ...
var mockPluginExecContext = new MockedPluginExecutionContext();
var mockProvider = Substitute.For<IServiceProvider>();
mockProvider.GetService<IPluginExecutionContext>.Returns(mockPluginExecContext);

// act
objectUnderTest.Execute(mockProvider);

// assert
Assert.IsTrue(...);