factory-girl-sync
factory-girl-sync
is a fork of Factory-girl that creates models synchronously.
Installation
Node.js:
npm install factory-girl-sync
To use factory-girl-sync
in the browser or other JavaScript environments, there are
builds for numerous module systems in the dist/
directory.
Usage
Refer to the tutorial for a gentle introduction of building a simple user factory.
Here's the crash course:
const factory = factory;const User = ; factory; factory;
Defining Factories
Define factories using the factory.define()
method.
For example:
// Using objects as initializerfactory;factory;
// Using functions as initializerfactory; // buildOptions can be passed while requesting an objectfactory;
Options
Options can be provided when you define a factory:
factory;
Alternatively you can set options for the factory that will get applied for all model-factories:
factory;
Currently the supported options are:
afterAttrs: function(modelAttrs, attrs, buildOptions)
Provides a function that is called after the model attributes are generated. The function should return the attributes or a Promise for the attrs.
afterBuild: function(model, attrs, buildOptions)
Provides a function that is called after the model is built. The function should return the instance or a Promise for the instance.
beforeCreate: function(model, attrs, buildOptions)
Provides a function that is called before a new model instance is saved. The function should return the instance or throw an error. For asynchronous functions, it should return a promise that resolves with the instance or rejects with the error.
afterCreate: function(model, attrs, buildOptions)
Provides a function that is called after a new model instance is saved. The function should return the instance or throw an error. For asynchronous functions, it should return a promise that resolves with the instance or rejects with the error.
factory;
Extending Factories
You can extend a factory using #extend
:
factory;factory;factory;
#extend(parent, name, initializer, options = {})
The #extend
method takes the same options as #define
except you
can provide a different Model
using options.model
.
Using Factories
Factory#attrs
Generates and returns model attributes as an object hash instead of the model instance. This may be useful where you need a JSON representation of the model e.g. mocking an API response.
factory; factory; factory;
You can use Factory#attrsMany
to generate a set of model attributes
factory;
Factory#build
Builds a new model instance that is not persisted.
factory;
The buildMany
version builds an array of model instances.
factory;
Similar to Factory#attrs
, you can pass attributes to override or buildOptions.
Factory#create(name, attrs, buildOptions)
Builds a new model instance that is persisted.
factory;
Factory#createMany(name, num, attrs, buildOptions = {})
The createMany version creates an array of model instances.
factory;
Similar to Factory#attrs
and Factory#build
, you can pass attrs
to override and
buildOptions
. If you pass an array of attrs
then each element of the array will be
used as the attrs for a each model created.
Factory#createMany(name, attrs, buildOptions = {})
If you can pass an array of attrs
then you can omit num
and the length of the array
will be used.
Factory#cleanUp
Destroys all of the created models. This is done using the adapter's destroy
method.
It might be useful to clear all created models before each test or testSuite.
Adapters
Adapters provide support for different databases and ORMs. Adapters can be registered for specific models, or as the 'default adapter', which is used for any models for which an adapter has not been specified. See the adapter docs for usage, but typical usage is:
const FactoryGirl = ;const factory = FactoryGirlfactory;const adapter = ; // use the mongoose adapter as the default adapterfactory; // Or use it only for one model-factoryfactory;
ObjectAdapter
ObjectAdapter
is a simple adapter that uses const model = new MyModel()
,
model.save()
and model.destroy()
.
factory; { // save the model } { // destroy the model }factory;
Creating new Factories
You can create multiple factories which have different settings:
let anotherFactory = ;anotherFactory; // use the Mongoose adapter
History
This module started out as a fork of
factory-lady, but the fork deviated quite a
bit. This module uses an adapter to talk to your models so it can support different ORMs
such as Bookshelf,
Sequelize,
JugglingDB, and
Mongoose (and doesn't use throw
for errors that might occur during save).
Version 4.0 is a complete rewrite with thanks to @chetanism.
License
Copyright (c) 2016 Chetan Verma.
Copyright (c) 2014 Simon Wade.
Copyright (c) 2011 Peter Jihoon Kim.
This software is licensed under the MIT License.