| Crates.io | fixture_derive |
| lib.rs | fixture_derive |
| version | 0.0.1 |
| created_at | 2025-05-04 19:33:45.03926+00 |
| updated_at | 2025-05-04 19:33:45.03926+00 |
| description | Fixture derive macro for Type Driven Development |
| homepage | https://github.com/ProbablyClem/fixture_rs |
| repository | https://github.com/ProbablyClem/fixture_rs |
| max_upload_size | |
| id | 1659882 |
| size | 5,501 |
You need to define a simple fixture trait in your repository
pub trait Fixture {
fn fixture() -> Self;
}
And then you can implement it for primitive types, or custom structs
impl Fixture for String {
fn fixture() -> Self {
"string".to_string()
}
}
impl Fixture for u32 {
fn fixture() -> Self {
1
}
}
impl<T: Fixture> Fixture for Option<T> {
fn fixture() -> Self {
Some(T::fixture())
}
}
impl<T: Fixture> Fixture for Vec<T> {
fn fixture() -> Self {
vec![T::fixture()]
}
}
impl<T: Fixture> Fixture for Box<T> {
fn fixture() -> Self {
Box::new(T::fixture())
}
}
```
Thanks to this crate, it can be automatically derived
```rust
#[derive(Fixture)]
pub struct User {
pub name: String,
pub age: u32,
pub bio: Option<String>,
}
#[derive(Fixture)]
pub struct Group {
pub users: Vec<User>,
}
```
You can then call fixture() to use it in your tests
```rust
#[test]
fn test_user_fixture() {
let user = User::fixture();
assert_eq!(user.name, "string".to_string());
assert_eq!(user.age, 1);
assert_eq!(user.bio, Some("string".to_string()));
}
#[test]
fn test_group_fixture() {
let group = Group::fixture();
assert_eq!(group.users.len(), 1);
let user = &group.users[0];
assert_eq!(user.name, "string".to_string());
}
```