#factory #fixture #test

factori

A factory library for Rust, inspired by FactoryBot. 🤖

7 releases (2 stable)

1.1.0 Aug 14, 2020
1.0.0 Jan 15, 2020
0.1.1 Jan 8, 2020
0.1.0 May 18, 2019
0.0.3 Jun 27, 2018

#231 in Testing

Download history 233/week @ 2023-12-23 1601/week @ 2023-12-30 1484/week @ 2024-01-06 972/week @ 2024-01-13 792/week @ 2024-01-20 819/week @ 2024-01-27 1007/week @ 2024-02-03 1018/week @ 2024-02-10 1010/week @ 2024-02-17 1006/week @ 2024-02-24 1464/week @ 2024-03-02 1257/week @ 2024-03-09 1662/week @ 2024-03-16 1522/week @ 2024-03-23 1257/week @ 2024-03-30 1164/week @ 2024-04-06

5,902 downloads per month
Used in slumber

MIT license

14KB
64 lines

Factori Build Status Crates.io Docs.rs

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.

Documentation

See API documentation.

Example

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.

Testing

Run:

cargo test

License

MIT

Dependencies

~1.5MB
~34K SLoC