A powerful mock object library for Rust that generates mock implementations of traits and structs for unit testing.
Mockall is a mock object library for Rust that enables comprehensive unit testing by creating mock implementations of traits and structs. It addresses the challenges of mocking in a statically typed language like Rust, providing a rich feature set with an ergonomic interface while being written in 100% safe and stable Rust.
Rust developers writing unit tests that require mocking dependencies, such as traits or structs, to isolate and test specific components of their application.
Developers choose Mockall for its automated mock generation via procedural macros, which reduces boilerplate, and its comprehensive feature set including expectation-based testing and support for mocking generic methods, all while maintaining safety and stability in Rust.
A powerful mock object library for Rust
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses procedural macros like #[automock] to automatically create mock implementations from traits, drastically reducing manual boilerplate code, as shown in the usage example where MockMyTrait is generated.
Allows precise control over mock behavior with methods like .with(), .times(), and .returning(), enabling comprehensive unit testing as demonstrated in the test snippet for argument matching and call counts.
Supports predicates such as eq() imported via predicate::* for argument matching, giving developers fine-grained control over which calls trigger mock responses in tests.
Capable of mocking generic methods and traits, addressing a common challenge in Rust mocking, inspired by designs like Simulacrum as noted in the README.
Built entirely with safe Rust code and maintains compatibility with stable Rust releases, ensuring reliability and adherence to Rust's safety guarantees, as emphasized in the overview.
Reliance on procedural macros increases compilation time, which can be a bottleneck in large codebases or during rapid test iterations, a common trade-off not explicitly mentioned but inherent to macro-based libraries.
Primarily designed for mocking traits and structs; mocking free functions or external crates without trait interfaces requires additional workarounds or may not be supported, limiting flexibility in some testing scenarios.
Setting up expectations for complex scenarios, such as sequences of calls or side effects, can involve verbose code and a steeper learning curve compared to simpler mocking tools.
Requires Rust 1.77.0 or higher as stated in the MSRV section, which might exclude projects stuck on older toolchains or environments with version constraints, potentially hindering adoption.