Skip to main content

HasFactory

Trait HasFactory 

Source
pub trait HasFactory: Sized {
    // Required method
    fn factory() -> Factory<Self>;
}
Expand description

A model with a factory.

Implemented by #[derive(Factory)], which needs Default — or by hand, when the default row is not a sensible starting point:

ⓘ
impl HasFactory for User {
    fn factory() -> Factory<Self> {
        Factory::new(|i| User {
            id: 0,
            email: format!("user{i}@example.com"),
            verified_at: None,
        })
    }
}

Writing it by hand is often the better answer for a model with unique columns: it puts the sequence in one place instead of in every test.

Required Methods§

Source

fn factory() -> Factory<Self>

A factory building one of these.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§