NyFactory class
Laravel-style model factories for testing.
This class allows you to define factories for creating test data with sensible defaults and overrideable attributes.
Example:
// Define a factory
NyFactory.define<User>((faker) => User(
id: faker.randomInt(1, 1000),
name: faker.name(),
email: faker.email(),
));
// Create instances
final user = NyFactory.make<User>();
final users = NyFactory.create<User>(count: 5);
// With overrides
final admin = NyFactory.make<User>(overrides: {'role': 'admin'});
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
clear(
) → void - Clear all factory definitions.
-
create<
T> ({int count = 1, Map< String, dynamic> ? overrides, List<String> ? states}) → List<T> -
Create multiple instances of type
T. -
define<
T> (T builder(NyFaker faker)) → void - Define a factory for a specific type.
-
defineWithOverrides<
T> (T builder(NyFaker faker, Map< String, dynamic> attributes)) → void - Define a factory with a callback that receives overrides.
-
getStates<
T> () → List< String> - Get list of available states for a type.
-
isDefined<
T> () → bool - Check if a factory is defined for a type.
-
make<
T> ({Map< String, dynamic> ? overrides, List<String> ? states}) → T -
Create a single instance of type
T. -
sequence<
T> (int count, T builder(int index, NyFaker faker)) → List< T> - Create instances with sequential data.
-
state<
T> (String name, T modifier(T instance, NyFaker faker)) → void - Define a state for a factory.