faux
![](https://github.com/nrxus/faux/workflows/test/badge.svg)
faux is a traitless mocking library for stable Rust. It was inspired by mocktopus, a mocking library for nightly Rust that lets you mock any function. Unlike mocktopus, faux deliberately only allows for mocking public methods in structs.
See the API docs for more information.
faux is in its early alpha stages, so there are no guarantees of API stability.
Setup
faux will modify existing code at compile time to transform structs
and their methods into mockable versions of themselves. faux makes
liberal use of unsafe Rust features, so it is only recommended for use
inside of tests. Add faux
as a dev-dependency in Cargo.toml
to
prevent usage in production code:
[]
= "0.0.3"
faux provides two attributes: create
and methods
. Use these
attributes for tagging your struct and its impl block
respectively. Use Rust's #[cfg_attr(...)]
to gate these attributes
to the test config only.
Usage
use crate UserClient;
// A sample #[test] for Service that mocks the client::UserClient
Due to constraints with rustdocs, the above example tests in
main()
rather than a #[test]
function. In real life, the faux
attributes should be gated to #[cfg(test)]
.
Goal
faux was founded on the belief that traits with single implementations are an undue burden and an unnecessary layer of abstraction. It aims to create mocks out of user-defined structs, avoiding extra production code that exists solely for tests. In particular, faux does not rely on trait definitions for every mocked object, which would pollute their function signatures with either generics or trait objects.