A Rust library for creating mocks from structs without requiring trait abstractions, designed for testing.
faux is a Rust library that creates mocks from structs for unit testing. It transforms structs and their methods into mockable versions, allowing developers to stub return values and behaviors without complicating production code with trait abstractions. It solves the problem of testing code that depends on external services or complex logic by providing a lightweight mocking solution.
Rust developers writing unit tests for applications or libraries that depend on structs with methods that need to be mocked, such as network clients, database connectors, or external service wrappers.
Developers choose faux because it avoids the overhead of trait-based mocking, allowing them to mock structs directly without polluting production code. It works on stable Rust, supports a wide range of method types (async, generic, trait methods), and integrates seamlessly with existing code using conditional compilation attributes.
Struct mocking 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.
Allows mocking structs directly without introducing trait abstractions into production code, keeping it clean as per its philosophy to avoid single-implementation traits.
Supports async methods, trait methods, generic methods, and pointer self types, making it versatile for various Rust testing scenarios outlined in the features.
Uses #[cfg_attr(test)] to gate mocking attributes to tests only, preventing leakage into production code, as recommended in the getting started guide.
Mocks automatically implement Send and Sync if the real instance does, and work with derived Clone, Debug, and Default traits, ensuring seamless integration.
Relies on unsafe Rust features, which, while confined to tests, may raise security or stability concerns for teams prioritizing memory safety in all code.
The order of macro expansion is unspecified, leading to potential conflicts with other attribute macros that modify method signatures, as admitted in the interactions section.
Only supports Clone, Debug, and Default for derivable traits; other traits like Eq or Hash are not supported, and Clone behavior shares stubs, which can be restrictive.