Home

Awesome

makery.js

Build Status

Factory-style (via blueprints) object creation for tests. Inspired by Ruby Machinist.

Makery is a library that allows you to create objects with default values (and possibly overriding those) via the definition of blueprints.

Makery works really well for creating Backbone models, but that is not the only use case. Makery can be used to create any kind of objects from a constructor.

How to install (web)

This library has Underscore as a single dependency. Simply put the underscore script before this one.

How to install (node)

npm install makery

How to use

Makery has a short syntax for constructors that have a single options object literal as a parameter. This is a very common way to create "Models", "Views" and other entities in most frameworks out there.

Given a constructor function like this:

function MyConstructor(options) {
  this.id = options.id;
  this.aProperty = options.aProperty;
  this.someOtherProperty = options.someOtherProperty;
}

Define the blueprint for the constructor

Makery.blueprint(MyConstructor, {
  aProperty: "Default value for property",
  someOtherProperty: false
});

Now create the object

var obj = MyConstructor.make();
obj.aProperty; // "Default value for property"
obj.someOtherProperty; // false

You can override the defaults

var obj = MyConstructor.make({
  someOtherProperty: true
});
obj.aProperty; // "Default value for property"
obj.someOtherProperty; // true

You can use functions to build up the properties in the blueprint. This is useful for creating new instances each time for a property.

Makery.blueprint(MyConstructor, {
  aProperty: function() { return [1, 2, 3]; },
  someOtherProperty: false
});

var obj = MyConstructor.make();
obj.aProperty; // [1, 2, 3]

Inside the function properties in the blueprint, you have access to helper functions, available directly though this. Available helpers:

Makery.blueprint(MyConstructor, {
  id: function() { return this.unique(); }
});

var obj = MyConstructor.make();
obj.id; // 0
var anotherObj = MyConstructor.make();
anotherObj.id; // 1

Also there is an afterCreation hook available in the blueprints, to do any kind of action with the just created object.

Makery.blueprint(MyConstructor, {
  aProperty: "The value of this property",
  afterCreation: function(obj) {
    obj.otherProperty = "Another value";
  }
});

var obj = MyConstructor.make();
obj.aProperty; // "The value of this property"
obj.otherProperty; // "Another value"

Multiple blueprints for the same constructor can be defined. An unnamed blueprint will be the default one, but you can name blueprints and specify that name when making objects to use them. Named blueprints will also use the default blueprint as a fallback for not defined properties.

Makery.blueprint(MyConstructor, {
  aProperty: "This is the default blueprint",
  anotherProperty: "Still from the default blueprint"
});

Makery.blueprint(MyConstructor, "another blueprint", {
  aProperty: "This is another blueprint"
});

var obj = MyConstructor.make();
obj.aProperty; //"This is the default blueprint"
obj.anotherProperty; //"Still from the default blueprint"

obj = MyConstructor.make("another blueprint");
obj.aProperty; //"This is another blueprint"
obj.anotherProperty; //"Still from the default blueprint"

Makery has a little more verbose way to define a blueprint, by passing a function as the last parameter of the definition. The return value of that function must be the parameter to use for the constructor.

Makery.blueprint(MyConstructor, function() {
  return {
    aProperty: "Some value",
    anotherProperty: "Another value"
  };
});

var obj = MyConstructor.make();
obj.aProperty; //"Some value"
obj.anotherProperty; //"Another value"

You can use this inside the function to get access to the helpers provided.

Makery.blueprint(MyConstructor, function() {
  return {
    id: this.unique(),
    aProperty: "Some value",
    anotherProperty: "Another value"
  };
});

var obj = MyConstructor.make();
obj.id; //1
obj.aProperty; //"Some value"
obj.anotherProperty; //"Another value"

With this "more verbose" way to define blueprints, you can work with constructors that have more than one parameter. To do this, just return an array with the parameters in the correct order for that constructor.

function MyMultiParamConstructor(param1, param2) {
  this.param1 = param1;
  this.param2 = param2;
}

Makery.blueprint(MyMultiParamConstructor, function() {
  return ["some param1 value", "some param2 value"];
});

var obj = MyMultiParamConstructor.make();
obj.param1; //"some param1 value"
obj.param2; //"some param2 value"

Changelog

v0.2.0

v0.1.0