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 =Monky =monky = mongoose;monky;// Set up factory without callback (throws)monky;
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:
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:
Referring to the specific path is also supported:
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
If you don't need the child instance to be populated but only saved, you can pass
build function builds new mongoose models based on the set up factories without saving them.
You can also set attributes when building, to replace default values
build to set up a new model instance and actually saves it.
You can also set attributes when creating, to replace default values
As of version 0.0.4 one can build and create lists of documents:
Since overwriting factories is not allowed, factories can be reset either completely or for a certain factory only:
monky; // Reset allmonky; // 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:
/** Model file*/var mongoose = ;var Schema = username: type: 'string' unique: true required: true ;mongoose;/** Pre-test file (setup)*/var mongoose =Monky =monky = mongoose;monky;// Mongoose connect and other setup stuffmoduleexportsmonky = monky;/** Actual test*/var monky = monky;;// Alternatively one can create a new user using before hook
As of version
0.6.0 Monky also fully supports promises for all public facing
build/create functions, e.g.:
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 email@example.com
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.