使用国际奥委会单元测试 [英] Using IoC for Unit Testing

查看:100
本文介绍了使用国际奥委会单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能IoC容器可用于单元测试?它是有用的管理在一个巨大的溶液(50 +项目)使用IoC的嘲弄?任何经验?对于在单元测试中使用它做工精良任何C#库?

How can a IoC Container be used for unit testing? Is it useful to manage mocks in a huge solution (50+ projects) using IoC? Any experiences? Any C# libraries that work well for using it in unit tests?

推荐答案

一般来说,因为单元测试是所有关于分离责任DI容器应该没有必要进行单元测试。

Generally speaking, a DI Container should not be necessary for unit testing because unit testing is all about separating responsibilities.

考虑使用构造器注入类

public MyClass(IMyDependency dep) { }

在整个应用程序,它可能是有一个巨大的依赖关系图背后隐藏的 IMyDependency ,但在一个单元测试,你压扁一切归因于一个单一的<一HREF =htt​​p://msdn.microsoft.com/en-us/magazine/cc163358.aspx>测试双。

In your entire application, it may be that there's a huge dependency graph hidden behind IMyDependency, but in a unit test, you flatten it all down to a single Test Double.

可以使用动态嘲笑像Moq的或RhinoMocks的生成测试双,但它不是必需的。

You can use dynamic mocks like Moq or RhinoMocks to generate the Test Double, but it is not required.

var dep = new Mock<IMyDependency>().Object;
var sut = new MyClass(dep);

在某些情况下,自动嘲讽容器可以是不错的,但你并不需要使用相同的DI容器的生产应用程序使用。

In some cases, an auto-mocking container can be nice to have, but you don't need to use the same DI Container that the production application uses.

这篇关于使用国际奥委会单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆