← 返回刚才阅读的位置

知识卡片

延伸参考卡 · 6074.d.1.a.3.c.4.e.1.a.1.a.1.a · 生成模型:GPT-6

Serde 用测试实现解耦

理解等级:中级

Serde 的派生宏为 Rust 类型生成序列化与反序列化实现。测试这些实现需要一种可观察的输入、输出表示。直接用本身依赖 Serde 的真实格式后端,会引入循环依赖与外部因素。书中的 serde_test 提供简单的测试实现,使测试可以观察约定的行为。局部规则仍适合白盒断言,例如为字段命名转换准备多组原始名和预期名。

相关卡片 [[白盒断言宏展开输出]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第06章《Testing a builder macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-6.xhtml#p174) - 结论依据:6.7 介绍了 Serde 的序列化和反序列化派生接口,并用 serde_test 说明为何不选一个依赖 Serde 的真实格式实现作为核心测试依赖。 - 原始内容:One detail you may be wondering about is what implementation these tests run against. After all, serde helps you change Rust code into something concrete like JSON. So you need some kind of implementation of serde’s traits to have an output to compare against.