mockall
A powerful mock object library for Rust unit testing.
Repository Health
Technical Analysis
Mockall is a feature-rich mock object library for Rust that lets you generate test doubles for traits and structs with a terse, ergonomic API. Mock objects share the interface of a real object but return responses your test code controls, letting you test upper layers of an application in isolation and inject error and edge cases that are hard to reproduce with the full stack.
Mocking is inherently harder in a statically typed language, and Mockall solves it by combining derive-style code generation (#[automock]) with an explicit mock! macro for cases the attribute cannot cover. It supports generic methods, associated types, static methods, sequences, and expectation matching — all in 100% safe, stable Rust.
What You Get
- The
#[automock]attribute to auto-generate a mock from a trait - The
mock!macro for mocking structs and cases automock cannot reach - Expectation matching on arguments via composable predicates
- Control over return values, call counts, and call sequencing
- Support for generic methods, associated types, and static methods
Common Use Cases
- Unit testing business logic without its real dependencies
- Injecting error and edge cases that are hard to trigger live
- Verifying that collaborators are called with expected arguments
- Asserting the order and number of calls to a dependency
Under The Hood
Architecture — Mockall is a Cargo workspace of three crates: mockall is the runtime/public facade users import, mockall_derive is the procedural macro crate that does the heavy lifting, and mockall_double is a small helper for swapping real and mock types under cfg(test). The derive crate’s src/ is organized by what it mocks — mockable_item.rs, mockable_struct.rs, mock_trait.rs, mock_item_struct.rs, mock_function.rs, and automock.rs — parsing the annotated item and code-generating a corresponding MockXxx type with expectation plumbing.
Tech Stack — Written in 100% safe, stable Rust (MSRV 1.77). It builds on syn/quote/proc-macro2 for macro expansion in mockall_derive, and on the predicates crate to power argument matching in the public API. The workspace uses resolver 2 and pins dependencies for a checked-in MSRV lockfile.
Code Quality — The project is mature and well tested, with integration examples in mockall/src/examples.rs, a detailed CHANGELOG, CODEOWNERS, and CI across multiple toolchains including an explicit MSRV job. Being pure safe/stable Rust with no nightly requirements is a deliberate quality goal stated in the README.
API Design — Ergonomics are the selling point: #[automock] turns a trait into a mock with almost no ceremony, while the mock! macro provides an escape hatch for structs and complex generics. Expectations read fluently (expect_foo().with(eq(4)).times(1).returning(...)), and predicate composition keeps argument matching expressive. The main learning curve is knowing when automock suffices versus when the more verbose mock! macro is required.
Used by 4 apps in this directory
iii
Developer Tools · Devops
Compose, extend, and observe every backend service in real time using three primitives: Workers, Functions, and Triggers.
Lemmy
Community · Social Media
Federated, self-hosted Reddit alternative with full community ownership and no corporate control.
Memgraph
Databases · AI Development
High-performance in-memory graph database for AI context and real-time analytics
OpenBB
Databases · Analytics · Invoicing Finance
The AI Workspace for Finance: Connect Data, Run AI Agents, Build Analytics