mockbuilder
Easily mock objects and instances of objects in Javascript using the Java builder concept
Installation
npm install mockbuilder --save
Usage
const builder = ; const johnBuilder = ; const jamesBuilder = johnBuilder ; const james = jamesBuilder; // { firstname: 'James', lastname: 'Smith' } const john = johnBuilder; // { firstname: 'John', lastname: 'Smith }
It can also be used with constructors:
{ thisfname = fname; thislname = lname;} const personBuilder =
And since this package is for the purposes of testing mocked objects you can set spies for your functions:
const sinon = ; const peter = personBuilder ; peter;petergetFullNamecalled; // true
Note Notice that a third parameter was passed to the set
method. It expects a boolean (defaults to false
). If set to true
, when build
is called it will set the value for that key as the return value of the function that is passed in. This allows the set up of spies since each call to build
will return a new instance of sinon.spy
in this case.
If an array is passed in to set
with the third parameter set to true
, the builder will iterate through the array and set the value of a given index with the return value of the function at that index.
Testing
To test, just run:
npm test