dburles:factory

v1.1.0Published 8 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Meteor Factory

A package for creating test data or for generating fixtures.

Installation

$ meteor add dburles:factory

Table of Contents

Examples

Defining factories

1Authors = new Meteor.Collection('authors');
2Books = new Meteor.Collection('books');
3
4Factory.define('author', Authors, {
5  name: 'John Smith'
6}).after(author => {
7  // Do something smart
8});
9
10Factory.define('book', Books, {
11  authorId: Factory.get('author'),
12  name: 'A book',
13  year() { return _.random(1900, 2014); }
14});
15
16// We can also extend from an existing factory
17Factory.define('anotherBook', Books, Factory.extend('book', {
18  // ...
19}));

Creating documents

1// Ex. 1: Inserts a new book into the books collection
2const book = Factory.create('book');
3
4// Ex. 2: New fields can be added or overwritten
5const book = Factory.create('book', { name: 'A better book' });

API

Note: 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.

define

Factory.define('name', Collection, doc).after(doc => { ... })

  • name
    • A name for this factory
  • Collection
    • A meteor collection
  • doc
    • Document object
  • .after hook (Optional)
    • Returns the newly inserted document after calling Factory.create

get

Factory.get('name')

Returns the instance of name. Typical usage is to specify a relationship between collections as seen in the Book example above.

build

Factory.build('name', doc)

Builds the data structure for this factory

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

tree

Factory.tree('name', doc)

Builds an object tree without _id fields. Useful for generating data for templates.

  • name
    • The name define for this factory
  • doc (Optional)
    • Document object

Example:

1  Factory.define('author', Authors, {
2    name: "John Smith"
3  });
4
5  Factory.define('book', Books, {
6    name: "A book",
7    author: Factory.get('author')
8  });
9
10  const book = Factory.tree('book');

book then equals:

{
  name: 'A book',
  author: {
    name: 'John Smith'
  }
}

create

Factory.create('name', doc)

Creates (inserts) this factory into mongodb

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

extend

Factory.extend('name', doc)

Extend from an existing factory

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

Other

Fake makes a great companion package. See https://atmospherejs.com/anti/fake

License

MIT. (c) Percolate Studio

factory was developed as part of the Verso project.