← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 303.a.3.d.1 · 生成模型:GPT-6

注入替身验证调用契约

理解等级:中级

将外部读写放在窄 trait 后,业务逻辑依赖接口而非全局资源。测试设置关键请求、返回值或错误,并检查业务行为。应保留真实适配器的单独验证,避免假实现和实际接口语义长期漂移。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Chapter 11. Testing 依赖注入与同步替身 · 查看原书依据

      For us, we pass in a struct that has implemented the trait, and then we call that trait. This is powerful. For example, we could implement a read trait for a struct that accesses a database for the read function. However, we could get a different struct that handles the reading from files and also implement the read trait. Depending on the storage solution we

    参考来源

    - 位置:《Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)》第11章《Chapter 11. Testing》,依赖注入与同步替身(来源项目 book-source,extracted/Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)/_epub-src/OEBPS/ch11.html#id69) - 结论依据:同一个 trait 可连接数据库、文件或 Mock,并核对调用参数与返回结果。 - 原始内容:For us, we pass in a struct that has implemented the trait, and then we call that trait. This is powerful. For example, we could implement a read trait for a struct that accesses a database for the read function. However, we could get a different struct that handles the reading from files and also implement the read trait. Depending on the storage solution we