Crates.io | factori |
lib.rs | factori |
version | 1.1.0 |
source | src |
created_at | 2018-06-14 13:53:40.54455 |
updated_at | 2020-08-14 21:52:23.894166 |
description | A factory library for Rust, inspired by FactoryBot. 🤖 |
homepage | |
repository | https://github.com/mjkillough/factori |
max_upload_size | |
id | 70089 |
size | 19,698 |
A testing factory library for Rust, inspired by FactoryBot. 🤖 🦀
Factori makes it easy to instantiate your test objects/fixtures in tests while providing an ergonomic syntax for defining how they are instantiated.
Factori works on stable Rust >=1.30.
See API documentation.
Factori provides two macros: factori!
, which defines a factory for a type,
and create!
which instantiates it:
#[macro_use]
extern crate factori;
pub struct Vehicle {
number_wheels: u8,
electric: bool,
}
factori!(Vehicle, {
default {
number_wheels = 4,
electric = false,
}
mixin bike {
number_wheels = 2,
}
});
fn main() {
let default = create!(Vehicle);
assert_eq!(default.number_wheels, 4);
assert_eq!(default.electric, false);
// Its type is Vehicle, nothing fancy:
let vehicle: Vehicle = default;
let three_wheels = create!(Vehicle, number_wheels: 3);
assert_eq!(three_wheels.number_wheels, 3);
let electric_bike = create!(Vehicle, :bike, electric: true);
assert_eq!(electric_bike.number_wheels, 2);
assert_eq!(electric_bike.electric, true);
}
More examples are available in the
tests/
directory.
Run:
cargo test
MIT