#factory #fixture #test #define

macro factori-impl

You shouldn't use this crate directly. It's internal to factori.

4 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

#14 in #fixture

Download history 339/week @ 2023-12-22 1301/week @ 2023-12-29 1441/week @ 2024-01-05 1154/week @ 2024-01-12 791/week @ 2024-01-19 877/week @ 2024-01-26 1001/week @ 2024-02-02 957/week @ 2024-02-09 1060/week @ 2024-02-16 985/week @ 2024-02-23 1425/week @ 2024-03-01 1171/week @ 2024-03-08 1692/week @ 2024-03-15 1750/week @ 2024-03-22 1150/week @ 2024-03-29 1042/week @ 2024-04-05

5,825 downloads per month
Used in 2 crates (via factori)

MIT license

14KB
356 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.45.

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