简体   繁体   English

在单元测试期间,如何使用MEF解决依赖关系?

[英]How can I use MEF to resolve dependencies during unit testing?

The component I'm developing has to interface with several external systems. 我正在开发的组件必须与多个外部系统交互。 I don't want to interact with the external systems when I'm running unit tests, just when we're doing integrated testing (or production, of course!) :-) In its most ugly-looking-form, it would be something along the lines of: 我不想在运行单元测试时与外部系统进行交互,而只是在我们进行集成测试(当然,就是生产!)时:-)在看起来最丑陋的形式中,类似于以下内容:

if (<Flag in App.config> == true)
{
    // get result from external system
}
else
{
    // get a mocked-up result
}

Is there an example of a better way to implement something like this using dependency injection so my code doesn't have to be polluted with IF-statements? 有没有一个更好的方法示例来使用依赖注入实现类似的东西,这样我的代码就不必被IF语句污染了? I was looking at using MEF because it's included in the .NET Framework. 我一直在使用MEF,因为它包含在.NET Framework中。

MEF is not a framework for unit testing, it is for extensiblily, to let others write plug-in for your software. MEF不是用于单元测试的框架,它是可扩展的,允许其他人为您的软件编写插件。

As I think, you should look into dependency IoC containers, here's a list of IoCs for .NET. 我认为,您应该研究依赖IoC容器,这是.NET的IoC 列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM