Mongoose fixtures library inspired by factory_girl
Inspired by factory_girl this library tries to bring similar functionality to node. The purpose is to avoid boilerplate code in your tests by creating models again and again, which can lead to very messy test suites when it comes to model with quite some columns.
$ npm install monky
monky into your project's
monky is separated into two steps: set up a factory (once) and use the factories to create models (unlimited).
In order to build/create new documents, a factory needs to be set up for each model that will be used. This should be done in some kind of pre-test-suite-file or maybe in a global
var mongoose = require'mongoose'Monky = require'monky'monky = mongoose;monkyfactory'User' username: 'name'// ..;// Set up factory without callback (throws)monkyfactory'User' username: 'name' ;
The above code set's up a new factory for the model named User. Each model that will be created from this factory will have an username which is set to name. All other paths will be empty.
Of course this is not enough to make documents valid. Therefore
monky comes with a sequence function which replaces the String "#n" with a sequence number. So in order to have a unique username, one could use the following example:
monkyfactory'User' username: '#n name' ;
This will lead to users with usernames like "1 name", "2 name" and so on. Embedded documents are also supported.
Monky also supports references between documents:
monkyfactory'User' username: 'foo' ;monkyfactory'Message' user: monkyref'User' ;monkybuild'Message'console.logmessageuserusername; // 'foo';
Referring to the specific path is also supported:
monkyfactory'User' username: 'bar' ;monkyfactory'Message' user_id: monkyref'User' 'id' ;monkybuild'Message'console.logmessageuser_id; // 53c6e964002c233e013ff4f8;
The according factory must be defined before it can be used in a reference.
If you have an existing instance you can pass it as value to
monkycreate'User'monkycreate'Message' user: userconsole.logmessageuserusername; // ==> equals user.username;;
If you don't need the child instance to be populated but only saved, you can pass
monkycreate'User'monkycreate'Message' user: user_idconsole.logmessageuser; // ==> equals user.id;;
build function builds new mongoose models based on the set up factories without saving them.
monkybuild'User'// user ==> mongoose user model with pre set data;
You can also set attributes when building, to replace default values
monkybuild'User' city: 'California' email: 'firstname.lastname@example.org'// user ==> mongoose user model with pre set data but with custom values
build to set up a new model instance and actually saves it.
monkycreate'User'// user ==> saved mongoose user model instance;
You can also set attributes when creating, to replace default values
monkycreate'User' city: 'California' email: 'email@example.com'// user ==> saved mongoose user model with custom values
As of version 0.0.4 one can build and create lists of documents:
monkybuildList'User' 5// users ==> array of 5 unsaved documents;monkycreateList'User' 5// users ==> array of 5 saved documents;
Since overwriting factories is not allowed, factories can be reset either completely or for a certain factory only:
monkyreset; // Reset allmonkyreset'User'; // Reset only 'User' factory
Monky uses the given factory name and maps it to the according model in mongoose. If you, however, want to access factories with a different name, you can tell monky which model to use:
monkyfactory name: 'Admin' model: 'User' username: username ;monkybuild'Admin'// admin ==> is a User, accessed as Admin;
/** Model file*/var mongoose = require'mongoose';var Schema = username: type: 'string' unique: true required: true ;mongoosemodel'User' Schema;/** Pre-test file (setup)*/var mongoose = require'mongoose'Monky = require'monky'monky = mongoose;monkyfactory'User' username: '#n name' ;// Mongoose connect and other setup stuffmoduleexportsmonky = monky;/** Actual test*/var monky = require'../setup'monky;describe'User'it'should not save without username'monkybuild'User'username = undefined;usersave// Expect err;;;it'should save user with valid data'monkybuild'User'usersavedone;;;it'should return computed amount of order'monkycreate'User'var amount = usergetComputedOrderAmount;// Do some checks here...;;;// Alternatively one can create a new user using before hookdescribe'User'beforeEachvar suite = this;monkycreate'User'if err return doneerr;suiteuser = user;done;;;;
As of version
0.6.0 Monky also fully supports promises for all public facing
build/create functions, e.g.:
monkybuild'User'then// user ==> built doc// err ==> triggered error;
Note: Monky uses
mpromise for promise handling, as does
current implementation of
mpromise uses node 0.10.x APIs, making the promise
Monky not suitable for prior versions. If you're still using node
0.8.x, use the callback interface instead.
Install dev dependencies and run the tests.
$ npm i$ make test
(The MIT License)
Copyright (c) 2012 Mario Behrendt firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.