A package for creating test data or for generating fixtures.
$ meteor add dburles:factory
Authors = new Meteor.Collection('authors');
Books = new Meteor.Collection('books');
Factory.define('author', Authors, {
name: 'John Smith'
}).after(function(author) {
// Do something smart
});
Factory.define('book', Books, {
authorId: Factory.get('author'),
name: 'A book',
year: function() { return _.random(1900, 2014); }
});
// We can also extend from an existing factory
Factory.define('anotherBook', Books, Factory.extend('book', {
// ...
}));
// Inserts a new book into the books collection
var book = Factory.create('book');
// New fields can be added or overwritten
var book = Factory.create('book', { name: 'A better book' });
- name
- A name for this factory
- Collection
- A meteor collection
- doc
- Document object
- .after hook (Optional)
- Returns the newly inserted document
Returns the instance of name. Typical usage is to specify a relationship between collections as seen in the Book example above.
When calling Factory.create('book')
both the Book and an Author are created. The newly created Author _id
will then be automatically assigned to that field. In the case of calling Factory.build('book')
as no insert operations are run, the _id
will be faked.
Builds the data structure for this factory
- name
- The name defined for this factory
- doc (Optional)
- Document object
Creates (inserts) this factory into mongodb
- name
- The name defined for this factory
- doc (Optional)
- Document object
Extend from an existing factory
- name
- The name defined for this factory
- doc (Optional)
- Document object
Fake makes a great companion package. See https://atmospherejs.com/anti/fake
MIT. (c) Percolate Studio
factory was developed as part of the Verso project.