factory-mate
Advanced tools
Comparing version 1.0.2 to 1.1.0
export * from './factoryMate/FactoryMateAware'; | ||
export * from './factoryMate/FactoryMate'; | ||
export * from './factoryMate/generators/NumberGenerator'; |
@@ -8,1 +8,2 @@ "use strict"; | ||
__export(require("./factoryMate/FactoryMate")); | ||
__export(require("./factoryMate/generators/NumberGenerator")); |
{ | ||
"name": "factory-mate", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "TypeScript library for building domain objects", | ||
@@ -9,3 +9,7 @@ "keywords": [ | ||
"factory", | ||
"builder" | ||
"builder", | ||
"tdd", | ||
"bdd", | ||
"test", | ||
"fixture" | ||
], | ||
@@ -12,0 +16,0 @@ "main": "./dist/index.js", |
# FactoryMate | ||
[![Build Status](https://travis-ci.org/rwaskiewicz/factory-mate.svg?branch=develop)](https://travis-ci.org/rwaskiewicz/factory-mate) | ||
[![npm version](https://badge.fury.io/js/factory-mate.svg)](https://badge.fury.io/js/factory-mate) | ||
@@ -86,2 +87,49 @@ FactoryMate is a TypeScript-based fixture library for instantiating domain objects for testing purposes, inspired by | ||
### Sequence Generation | ||
FactoryMate supports numerical sequence generation via the ```NumberGenerator``` class. This can be helpful for the purposes of generating ID values for domain objects to better represent real world scenarios (e.g. keys in from a datastore) | ||
In order to add sequential generation support to an entity, it can be imported into it's factory as such: | ||
``` typescript | ||
import { FactoryMate, FactoryMateAware } from 'factory-mate'; | ||
import { NumberGenerator } from 'factory-mate'; | ||
import { GroceryItem } from './GroceryItem'; | ||
@FactoryMateAware | ||
export class GroceryItemFactory { | ||
public define() { | ||
// Defines the NumberGenerator instance to be used across all calls to FactoryMate.build(GroceryItem.name); | ||
const numberGenerator = new NumberGenerator(); | ||
FactoryMate.define(GroceryItem, (): GroceryItem => { | ||
const groceryItem = new GroceryItem(); | ||
// The nextValue() method retrieves the next value in the sequence | ||
groceryItem.id = numberGenerator.nextValue(); | ||
groceryItem.groceryName = 'chewy cookies'; | ||
return groceryItem; | ||
}); | ||
} | ||
} | ||
``` | ||
Using the factory method above, three sequential calls to ```FactoryMate.build(GroceryItem.name)``` will result in the following: | ||
``` typescript | ||
const groceryItem1: GroceryItem = FactoryMate.build(GroceryItem.name); | ||
const groceryItem2: GroceryItem = FactoryMate.build(GroceryItem.name); | ||
const groceryItem3: GroceryItem = FactoryMate.build(GroceryItem.name); | ||
console.log(JSON.stringify(groceryItem1)); //'{"id":1,"groceryName":"chewy cookies"}' | ||
console.log(JSON.stringify(groceryItem2)); //'{"id":2,"groceryName":"chewy cookies"}' | ||
console.log(JSON.stringify(groceryItem3)); //'{"id":3,"groceryName":"chewy cookies"}' | ||
``` | ||
#### Changing sequence values | ||
By default, ```NumberGenerator``` starts at a value of 1 and increments by 1. These values can be altered at instantiation time if desired | ||
``` typescript | ||
// Start at one, increment by one: 1, 2, 3 ... | ||
const numberGenerator = new NumberGenerator(); | ||
// Start at one, increment by two: 1, 3, 5 ... | ||
const numberGenerator = new NumberGenerator(1, 2); | ||
// Start at zero, increment by one: 0, 1, 2, ... | ||
const numberGenerator = new NumberGenerator(0); | ||
``` | ||
## Example | ||
@@ -88,0 +136,0 @@ An example project using FactoryMate can be found here [FactoryMateConsumer](https://github.com/rwaskiewicz/factory-mate-consumer) |
Sorry, the diff of this file is not supported yet
15
107
138
12038