单元测试IoC注册?

编程入门 行业动态 更新时间:2024-10-22 20:40:35
本文介绍了单元测试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注册?

本文发布于:2023-11-14 19:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   IoC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!