#factory #fixture #instantiation #test

factori-imp

factori-imp(roved), a factory library for Rust, inspired by FactoryBot. 🤖

4 releases

0.9.3 Oct 1, 2024
0.9.2 Sep 3, 2024
0.9.1 Sep 3, 2024
0.9.0 Sep 1, 2024

#196 in Testing

Download history 335/week @ 2024-08-31 31/week @ 2024-09-07 50/week @ 2024-09-14 12/week @ 2024-09-21 189/week @ 2024-09-28 295/week @ 2024-10-05 197/week @ 2024-10-12 159/week @ 2024-10-19 250/week @ 2024-10-26 90/week @ 2024-11-02 207/week @ 2024-11-09 211/week @ 2024-11-16 99/week @ 2024-11-23 209/week @ 2024-11-30

731 downloads per month

MIT license

18KB
81 lines

factori-imp(roved)

A testing factory library for Rust, inspired by:

A fork of mjkillough/factori library, to add additional features.

factori-imp(roved) makes it easy to instantiate your test objects/fixtures in tests while providing an ergonomic syntax for defining how they are instantiated.

factori-imp works on stable Rust >=1.45.

Differences with factori

  • Transient attributes as first class citizens, see tests/transient.rs
  • Adds create_vec! macro, see tests/create_vec.rs
  • create* macros can be used in factory declarations, see tests/simple.rs:34
    • Contributed by @wuerges, thank you
  • Fixes all clippy warnings due to usage of the macros

Documentation

https://docs.rs/factori-imp/latest/factori_imp/

In place replacement for factori

You can use factori-imp without changing any code, in your Cargo.toml:

- factori = "1.1.0"
+ factori = { package = "factori-imp", version = "0.9.2" }

or just add it as a regular dependency and then rename the crate in your crate root:

extern crate factori-imp as factori;

Example

factori-imp(roved) provides three macros:

  • factori!, which defines a factory for a type
  • create! which instantiates it
  • create_vec! which instantiates many
#[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);

  // We can create many vehicles
  let many_motorcycles = create_vec!(Vehicle, 5, number_wheels: 2);
  assert_eq!(many_motorcycles.len(), 5);
}

More examples are available in the tests/ directory.

Testing

Install cargo-nextest

Run:

make test

License

MIT

Dependencies

~1.5MB
~37K SLoC