单元测试IoC注册? [英] Unittesting IoC registration?

查看:68
本文介绍了单元测试IoC注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否应该对将组件注册到IoC容器中的代码进行单元测试?

Should you unittest the code that registers components into your IoC container?

如果是,怎么办?

推荐答案

Guice IoC容器,首先是我在没有Guice的情况下使用TDD为某些功能生成了类(这些是单元测试).然后,我使用Guice为该功能创建了集成测试.此时,IoC配置(Guice模块)不完整,因此集成测试将失败.使用TDD,我逐步添加了IoC配置,直到通过集成测试为止.除非通过测试,否则我不会添加任何@Inject批注,配置行或范围声明.结果,我将进行集成(或验收)测试,以确保IoC配置正确且完整.

What I do with the Guice IoC container, is that first I produce the classes for some feature using TDD without Guice (these are unit tests). Then I create an integration test for that feature with Guice. At that point the IoC configuration (Guice module) is incomplete, so the integration test will fail. Using TDD, I add the IoC configuration step by step until the integration test passes. I won't add any @Inject annotation, line of configuration or scope declaration, unless it is required to pass a test. As a result, I will have integration (or acceptance) tests making sure that the IoC configuration is right and complete.

此方法适用于任何IoC容器或其他系统,这些容器的配置非常复杂,以至于可能会损坏-除非需要通过测试,否则请勿编写任何配置.

This same method should work with any IoC container or other system, whose configuration is so complex that it may break - don't write any configuration unless it is require to pass a test.

这篇关于单元测试IoC注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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