知识卡片
测试替身记录接口交互
当被测函数只产生副作用时,可把消息发送抽成 trait,将真实实现换成记录调用的 MockMessenger。执行 set_value(80) 后检查记录,比真正发通知更容易观察行为。若接口是 &self,可用 RefCell<Vec<String>> 安全地在测试替身内更新记录。
相关卡片 [[内部可变性延后借用检查]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 智能指针 A Use Case for Interior Mutability: Mock Objects · 查看原书依据
Sometimes during testing a programmer will use a type in place of another type, in order to observe particular behavior and assert that it’s implemented correctly. This placeholder type is called a test double. Think of it in the sense of a stunt double in filmmaking, where a person steps in and substitutes for an actor to do a particularly tricky scene.
参考来源
- 位置:《The Rust Programming Language(第2版)》第15章 智能指针(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c15.xhtml`,块 0245)
- 结论依据:原文区分一般测试替身与记录交互、供事后断言的 mock。
- 原始内容:Sometimes during testing a programmer will use a type in place of another type, in order to observe particular behavior and assert that it’s implemented correctly. This placeholder type is called a test double. Think of it in the sense of a stunt double in filmmaking, where a person steps in and substitutes for an actor to do a particularly tricky scene.