mockall

A powerful mock object library for Rust unit testing.

Library
Cargo
v0.15.0
1,832stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
56/100Fair
Development Activity48
Maintenance24
Community52
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture84
Code Quality85
Innovation82
Learning Curve62

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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search