Fixtures replacement and random data generator for mongoose.
mongoose-fakery provides an easy fixture replacement method and random data generators.
mongoose-fakery where inspired by:
- Clone the repo.
- Create a branch.
- Write awesome code.
- Add tests for your changes. Test dependencies are defined in
- Open a Pull Request.
- Receive a 'Thank you!' and possibly a digital beer from me.
- Associations with other models. This is my first priority.
- Browser compatibility.
- Add more data generators.
npm install mongoose-fakery
var mongoose = require'mongoose'Schema = mongooseSchema;var UserSchema =name: Stringsurname: String;mongoosemodel'User' UserSchema;
In your tests or fixture files:
var fakery = require'mongoose-fakery';fakeryfake'user' mongoosemodel'User'name: 'john'surname: 'doe';
If the only argument you pass into
fake() is the name of the factory then
the method becomes a getter.
var userFakery = fakeryfake'user';
'Lazy' attributes are attributes that are resolved during a 'second' pass over the attributes of the fakery. Common usage are attributes that depend on other attributes for their value.
To create a 'lazy' attribute use
fakeryfake'user' mongoosemodel'User'name: 'john'surname: 'doe'email: fakerylazy// this will return firstname.lastname@example.org attrsname + '@example.com';;;
Each lazy attribute receives all the resolved attributes of the first pass as the only parameter.
Data generators are functions that return data. That data can be random or follow
mongoose-fakery comes with a number of pre-defined data generators
which will probably suit most of your needs i.e:
- random strings (hex, alpha, alphanum)
- random numbers
- random booleans (true/false)
- lorem generator
- name, surname and gender
- picking random items from lists
Pre-defined data generators are exposed under the
g attribute of the
object. Take a look in
data_providers.js to see all the available generators
and their APIs.
// using the user model defined abovefakeryfake'user' mongoosemodel'User'name: fakerygnamesurname: fakerygsurname;
Generators can also be used in arrays and nested attributes:
fakeryfake'post' mongoosemodel'Post'name: fakerygname// this will create tags 'projects', <random string>, 'tech'tags: 'projects' fakerygstr5 'tech';
Data generators can also be used when you just want to generate a bunch of random data for whatever purpose. They are not specific to test factories:
var fakery = require'mongoose-fakery';// generate 10 random full namesvar names = i;for i = 0; i < 10; i++namespushfakerygfullname;
mongoose-fakery gives you the option to create custom generators that attach
themselves to the
g attribute to the
// declare like thisfakerygenerator'custom'return 'custom';;// use like thisvar customGenerator = fakerygcustom;customGenerator; // returns 'custom'
As you might have guessed, generators wrap 'data provider' methods in a function. You can thus do things like:
fakerygenerator'timesTwo'return n*2;;var timesTwo = fakerygtimesTwo;timesTwo2; // returns 4
To make a fake model, use the
make() can also receive overrides.
var model = fakerymake'user';var modelWithOverrides = fakerymake'user'name: 'override';
Note that the model is not saved to the database.
To make and save a fake model, use the
fakerymakeAndSave'user'// `user` is saved to the database at this point;fakerymakeAndSave'user' name: 'override'// `user` is saved to the database and name is overriden to 'override'.;