知识卡片
Result 测试传播错误
测试可返回 Result<(), E>,Ok(()) 表示通过,Err 表示失败,前置操作因而可写 ?。若用例预期业务函数返回 Err,应检查 is_err() 或匹配错误值。返回 Result 的用例与 #[should_panic] 分别使用。
相关卡片 [[分辨预期的 panic]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 编写自动化测试 Checking for Panics with should_panic · 查看原书依据
Writing tests so they return a Result<T, E> enables you to use the question mark operator in the body of tests, which can be a convenient way to write tests that should fail if any operation within them returns an Err variant.
参考来源
- 位置:《The Rust Programming Language(第2版)》第11章 编写自动化测试(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c11.xhtml`,块 0134)
- 结论依据:原文解释 `?` 会让操作返回的 `Err` 直接成为测试失败。
- 原始内容:Writing tests so they return a Result<T, E> enables you to use the question mark operator in the body of tests, which can be a convenient way to write tests that should fail if any operation within them returns an Err variant.