Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Struct mocking library for Rust
| Date | Stars |
|---|---|
| 2026-07-24 | 485 |
| 2026-07-25 | 485 |
| 2026-07-28 | 485 |
| 2026-07-30 | 485 |
| 2026-08-09 | 484 |
| 2026-09-20 | 484 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# faux   [![Latest Version]][crates.io] [![rustc 1.65+]][Rust 1.65] [![docs]][api docs] ![][build]
A library to create [mocks] out of structs.
`faux` allows you to mock the methods of structs for testing without
complicating or polluting your code.
See the [API docs] for more information.
## Getting Started
`faux` makes liberal use of unsafe Rust features, so it is only
recommended for use inside tests. To prevent `faux` from leaking into
your production code, set it as a `dev-dependency` in your
`Cargo.toml`:
```toml
[dev-dependencies]
faux = "^0.1"
```
`faux` provides two attributes:
* `#[create]`: transforms a struct into a mockable equivalent
* `#[methods]`: transforms the methods in an `impl` block into their
mockable equivalent
Use Rust's `#[cfg_attr(...)]` to gate these attributes to the test
config only.
```rust
#[cfg_attr(test, faux::create)]
pub struct MyStructToMock { /* fields */ }
#[cfg_attr(test, faux::methods)]
impl MyStructToMock { /* methods to mock */ }
```
## Examples
```rust
mod client {
// #[faux::create] makes a struct mockable and
// generates an associated `faux` function
// e.g., `UserClient::faux()` will create a mock `UserClient` instance
#[faux::create]
pub struct UserClient { /* data of the client */ }
#[derive(Clone)]
pub struct User {
pub name: String
}
// #[faux::methods] makes every public method in the `impl` block mockable
#[faux::methods]
impl UserClient {
pub fn fetch(&self, id: usize) -> User {
// does some network calls that we rather not do in tests
User { name: "".into() }
}
}
}
use crate::client::UserClient;
pub struct Service {
client: UserClient,
}
#[derive(Debug, PartialEq)]
pub struct UserData {
pub id: usize,
pub name: String,
}
impl Service {
fn user_data(&self) -> UserData {
let id = 3;
let user = self.client.fetch(id);
UserData { id, name: user.name }
}
}
// A sample #[test] for Service that mocks the client::UserClient
fn main() {
// create a mock of client::UserClient using `faux`
let mut client = client::UserClient::faux();
// mock fetch but only if the argument is 3
// argument matchers are optional
faux::when!(client.fetch(3))
// stub the return value for this mock
.then_return(client::User { name: "my user name".into() });
// prepare the subject for your test using the mocked client
let subject = Service { client };
// assert that your subject returns the expected data
let expected = UserData { id: 3, name: String::from("my user name") };
assert_eq!(subject.user_data(), expected);
}
```
**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)]`.**
## Features
`faux` lets you mock the return value or implementation of:
* Async methods
* Trait methods
* Generic struct methods
* Methods with pointer self types (e.g., `self: Rc<Self>`)
* Methods in external modules (but not external crates).
`faux` also provides easy-to-use argument matchers.
## Interaction with `#[derive(...)]` and auto-traits.
`faux` mocks will auto implement `Send` and `Sync` if the real
instance also implements it. Using `#[derive(...)]` for `Clone`,
`Debug`, and `Default` will also work as expected. Other derivable
traits are not supported as they are about data (e.g., `Eq`, or
`Hash`) but `faux` is about mocking behavior not data. Deriving traits
that are not part of the standard library is also not currently
supported. An escape hatch for this is to manually write the `impl`
for that trait. If you believe there is a derivable trait that `faux`
should support please file an issue explaining your use case.
`Clone` is a bit of a special case in that it does not duplicate the
stubs but instead shares them with the cloned instance. If this is not
the desired behavioExcerpt of 8,157 characters
Read on GitHubWould you bet a product on this? Bounded 0–100 and slow moving.
matched fp:a329392b42aabdb1, topic:testing