如何在测试中管理 IoC 容器?
我对测试和 IoC 容器非常陌生,并且有两个项目:
- MySite.Website (MVC)
- MySite.WebsiteTest
目前我的网站中有一个 IoC 容器。我应该为我的测试重新创建另一个 IoC 容器吗?或者有没有办法在两者中使用 IoC?
I'm very new to testing and IoC containers and have two projects:
- MySite.Website (MVC)
- MySite.WebsiteTest
Currently I have an IoC container in my website. Should I recreate another IoC container for my test? Or is there a way to use the IoC in both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试是关于实际实施的。所以你通常不应该在单元测试中使用 IOC。如果您确实觉得需要它(一个组件依赖于另一个组件),请使用接口来隔离交互并使用模拟库(起订量很好)来模拟它并进行测试。
我唯一一次看到 IOC 容器对于测试来说是必需的是在集成测试中。
Testing is about real implementation. So you normally should not use IOC in your unit tests. In case you really feel needing it (one component depending on another one), using an interface to isolate the interaction and using a mocking lib (Moq is good) to mock it and do the testing.
The only chance I see IOC container is necessary for testing is in integration testing.
当您拥有 IoC 容器时,希望您还会进行某种依赖注入 - 无论是通过构造函数还是 setter 注入。
单元测试的重点是单独测试组件,而 DI 对此有很大帮助。您想要做的是通过手动构造每个类并向其传递所需的依赖项来对每个类进行单元测试,而不是依赖容器来构造它。
这样做的目的很简单。您希望尽可能隔离 SUT(被测系统)。如果您的 SUT 依赖于另一个类和 IoC 来注入它,那么您实际上是在测试三个系统,而不是一个。
举个例子:
ApiController
构造函数是一个依赖构造函数,将在运行时被 IoC 容器调用。然而,在测试过程中,您需要模拟 IRequestParser 接口并手动构建控制器。When you have an IoC container, hopefully you will also have some sort of dependency injection going on - whether through constructor or setter injection.
The point of a unit test is to test components in isolation and doing DI goes a long way in aiding that. What you want to do is unit test each class by manually constructing it and passing it the required dependencies, not rely on container to construct it.
The point of doing that is simple. You want to isolate the SUT(System Under Test) as much as possible. If your SUT relies on another class and IoC to inject it, you are really testing three systems, not one.
Take the following example:
The
ApiController
constructor is a dependency constructor and will get invoked by IoC container at runtime. During test, however, you want to mockIRequestParser
interface and construct the controller manually.