|
所在平台: Udemy |
课程主页: https://www.udemy.com/course/unit-testing-with-rust-and-tokio/
课程评论:没有评论
**课程名称:** Rust与Tokio单元测试 **课程概述:** 学习如何为Rust项目编写真正意义上的单元测试。本课程将深入探讨在Rust中使用Tokio异步运行时进行单元测试的挑战,特别是如何在不修改现有代码的情况下,有效地模拟(Mock)依赖项,实现依赖注入,以及处理Traits的用法限制。 **主要内容:** * **Rust单元测试的挑战:** 阐述在Rust中实现完全模拟的单元测试的困难,尤其是在处理Traits和异步代码时。 * **依赖注入与Mocking:** 学习利用设计模式实现Rust中的依赖注入,并使用Mocking技术隔离待测试单元。 * **Traits与Mocking:** 解决Rust非面向对象特性给Traits和Mocking带来的挑战。 * **Tokio异步测试:** 掌握处理Tokio异步函数时测试同步和共享全局fixtures的技巧。 * **实用Crates:** 介绍有助于测试的Rust Crates,包括全局变量预设、服务Mocking以及随机值生成工具。 * **Bonus内容:** 演示如何利用Generics将Mock注入函数内部实现,从而在不修改原函数的情况下Mock内部调用。 **课程目标:** 通过本课程的学习,您将能够构建真正意义上经过Mocked的Rust单元测试,而非伪装成单元测试的集成测试,从而提升代码质量和测试效率。
Programming with Rust is challenging. Even after spending substantial time learning and building apps with Rust, creating fully Mocked Unit Tests can be quite difficult.A real Unit Test should have its calls Mocked and only allow the Unit Under Test to run real code. But how does one do this with Rust? Dependency Injection is kind of doable, but dealing with Traits and their usage constraints can sometimes feel like a wrestling match.Although Rust has its own form of Interface, Rust is not an Object Oriented Programming language and does not have inheritance-which can make it unclear as to how to use Traits to perform patterns like Dependency Injection and creation of test Mocks. In addition the usage of async functions through the Tokio Runtime adds additional complexity to our tests. How do I force my tests to wait until certain variables are set? How do I share fixtures that are set globally across individual tests?In this course we'll learn about various design patterns we can use to help build Rust based Unit Tests that are truly Mocked, and not Integration Tests masquerading as Unit Tests. We'll also learn about several crates that can help with things like pre-setting global variables to be used by our tests before the tests are run, mocking crates that can help us Mock out services, and utilities that can help set random values to add robustness to our tests.Bonus Content shows how to use Generics to inject our Mocks into the internal implementation of functions. This allows us to Mock internal calls within functions without actually modifying the working function itself.