For the complete documentation index, see llms.txt. This page is also available as Markdown.

Testing with Model Factories

Quick includes Laravel-inspired model factories under quick.resources.testing. Factories provide readable test data without putting test-only behavior on production entities.

Defining a Factory

Create factory components outside your production model folder. The conventional name is the entity mapping followed by Factory.

// tests/resources/factories/UserFactory.cfc
component extends="quick.resources.testing.Factory" {

    struct function definition() {
        return {
            "username" : "user-#lCase( createUUID() )#",
            "firstName" : "Factory",
            "lastName" : "User"
        };
    }

    any function administrator() {
        return state( { "type" : "admin" } );
    }

}

Every factory must implement definition() and return the default attribute struct for one entity.

Factory Manager

Create a FactoryManager in your test base class. It resolves factory definitions and Quick entity providers through WireBox.

An optional context struct passed to the manager is available from each definition through getFactoryContext().

Making and Creating Entities

make() returns an unsaved Quick entity. create() persists through the entity's normal save() lifecycle.

Explicit attributes override the definition and any states:

Use count() to return an array of entities:

States

States can be structs or closures. A state closure receives the attributes accumulated so far and a context containing a zero-based index and the requested count.

Named factory methods can return state() and participate anywhere in a fluent chain:

Sequences

sequence() cycles through an array of state structs or closures.

Attribute values may also be closures. They are evaluated after states and receive the accumulated attributes and factory context.

Callbacks

Use afterMaking() and afterCreating() for additional test setup. Each callback receives the entity and its evaluated attributes.

Register callbacks for every use of a factory by overriding configure() in the factory definition:

All implementation classes live under resources/testing. Production packaging can exclude that directory; Quick does not load or register factory classes during normal module startup.

Last updated

Was this helpful?