← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 3498.a.2

依赖注入让测试替身可替换

理解等级:高级

把客户行为与具体依赖的创建分开,通过接口接收外部注入的服务,实现不必知道怎样被测试。测试可注入能管理和观察状态的替身,而不用改功能代码。代价是前期结构复杂度和运行时选择的不确定性;应验证实际注入的实现,不能仅测替身便认为生产依赖正确。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 可测试性 · 查看原书依据

      Test instances can be injected (rather than production instances), and these test instances can manage and monitor the state of the service. Thus, the client can be written with no knowledge of how it is to be tested. This is, in fact, how many modern testing frameworks are implemented.

    参考来源

    - 位置:《Software Architecture in Practice, 4th Edition》第12章《可测试性》(来源项目:book-sources;源文件:extracted/Software Architecture in Practice, 4th Edition/_epub-src/ch12.html) - 结论依据:所引连续原文给出本卡机制或取舍;条件与示例结合本节已读上下文解释。 - 原始内容:Test instances can be injected (rather than production instances), and these test instances can manage and monitor the state of the service. Thus, the client can be written with no knowledge of how it is to be tested. This is, in fact, how many modern testing frameworks are implemented.