Factory-style (via blueprints) object creation for tests.
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.
This library has Underscore as a single dependency. Simply put the underscore script before this one.
npm install makery
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.
Define the blueprints for the constructor
MakeryblueprintMyConstructoraProperty: "Default value for property"someOtherProperty: false;
Now create the object
var obj = MyConstructormake;objaProperty; // "Default value for property"objsomeOtherProperty; // false
You can override the defaults
var obj = MyConstructormakesomeOtherProperty: true;objaProperty; // "Default value for property"objsomeOtherProperty; // 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.
MakeryblueprintMyConstructorreturn 1 2 3;someOtherProperty: false;var obj = MyConstructormake;objaProperty; // [1, 2, 3]
Inside the function properties in the blueprint, you have access to helper functions, available directly though
this. Available helpers:
unique: gives you an incremental value each time, ideal for ids or unique properties. Optionally pass a prefix, to create unique properties of the type of 'prefixid'.
MakeryblueprintMyConstructorreturn thisunique;;var obj = MyConstructormake;objid; // 0var anotherObj = MyConstructormake;anotherObjid; // 1
Also there is an
afterCreation hook available in the blueprints, to do any
kind of action with the just created object.
MakeryblueprintMyConstructoraProperty: "The value of this property"objotherProperty = "Another value";;var obj = MyConstructormake;objaProperty; // "The value of this property"objotherProperty; // "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.
MakeryblueprintMyConstructoraProperty: "This is the default blueprint"anotherProperty: "Still from the default blueprint";MakeryblueprintMyConstructor "another blueprint"aProperty: "This is another blueprint";var obj = MyConstructormake;objaProperty; //"This is the default blueprint"objanotherProperty; //"Still from the default blueprint"obj = MyConstructormake"another blueprint";objaProperty; //"This is another blueprint"objanotherProperty; //"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.
MakeryblueprintMyConstructorreturnaProperty: "Some value"anotherProperty: "Another value";;var obj = MyConstructormake;objaProperty; //"Some value"objanotherProperty; //"Another value"
You can use
this inside the function to get access to the helpers provided.
MakeryblueprintMyConstructorreturnid: thisuniqueaProperty: "Some value"anotherProperty: "Another value";;var obj = MyConstructormake;objid; //1objaProperty; //"Some value"objanotherProperty; //"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.
thisparam1 = param1;thisparam2 = param2;MakeryblueprintMyMultiParamConstructorreturn "some param1 value" "some param2 value";;var obj = MyMultiParamConstructormake;objparam1; //"some param1 value"objparam2; //"some param2 value"