approval-result-factory
v0.3.2
Published
Automatically build service fakes into an approvable result object
Downloads
31
Readme
approval-result-factory
What?
Approval result factory, builds an object that allows for the easier approval of complex interactions of service mocks.
This library is intended to be used with sinon and approvals. It also has support for stubcontractor a touch integration testing library.
Why?
When testing complex mock heavy interactions, there is a lot of boiler plate code used to get a nicely readable approvals result. This facory builds an object to stringify and use.
Note
This library works well with spyFactory.
addDatum
Add datum is the simplest of calls. It adds data to the result unconditionally.
let resultBuilder = resultBuilderFactory();
resultBuilder.addDatum('add string', 'value string');
const result = resultBuilder.getResult();
At this point the result looks like:
{
"add string": "value string"
}
addData
Add data is the next simplest call. It adds an array of arrays to the object.
let resultBuilder = resultBuilderFactory();
resultBuilder.addData(
[
['add string', 'value string'],
['added int', 5],
['added null', null],
]
);
const result = resultBuilder.getResult();
At this point the result looks like:
{
"add string": "value string",
"added int": 5,
"added null": null
}
addCall
Add call looks at a sinon spy method and conditionally adds it if it has been called.
let call1 = sinon.spy();
let call2 = sinon.spy();
call2('I called number 2');
let resultBuilder = resultBuilderFactory();
resultBuilder.addCall('first', call1);
resultBuilder.addCall('second', call2);
const result = resultBuilder.getResult();
Because only call2
was exersized the result looks like:
{
"second": [
[
"I called number 2"
]
]
}
addCalls
'addcalls' addes spies in an array of arrays.
let call1 = sinon.spy();
let call2 = sinon.spy();
let call3 = sinon.spy();
call1(1);
call3('call3');
let resultBuilder = resultBuilderFactory();
resultBuilder.addCalls(
[
['first', call1],
['second', call2],
['third', call3]
]
);
const result = resultBuilder.getResult();
The result now looks like:
{
"first": [
[
1
]
],
"third": [
[
"call3"
]
]
}
addFakeService
Add service fake takes an object with methods that are spies. It then conditionally addes each of those methods if they have been called.
NOTE
The fake service must have a property __name
which will contain the name of the module being faked.
const myFakeService = {
__name: 'calculator',
add: sinon.spy(),
subtraction: sinon.spy(),
multiplication: sinon.spy()
};
myFakeService.add(1, 2);
myFakeService.multiplication(2, 2);
let resultBuilder = resultBuilderFactory();
resultBuilder.addFakeService(myFakeService);
const result = resultBuilder.getResult();
Because subtraction
was not called the result looks like this:
{
"calculator.add": [
[
1,
2
]
],
"calculator.multiplication": [
[
2,
2
]
]
}
addFakeServices
Add fake services takes an array of fake services and conditionally addes each of their spyied on methods.
let fakeCalculator = {
__name: 'calculator',
add: sinon.spy(),
subtraction: sinon.spy(),
multiplication: sinon.spy()
};
let fakeCar = {
__name: 'car',
accelerate: sinon.spy(),
decelerate: sinon.spy(),
openWindow: function () {},
};
fakeCalculator.add(1, 2);
fakeCalculator.multiplication(2, 2);
fakeCar.accelerate('faster');
fakeCar.decelerate('slower');
fakeCar.openWindow();
resultBuilder.addFakeServices(
[
fakeCalculator,
fakeCar
]
);
Because the subtraction
method was not called and the openWindow
function is not a spy, the result is the followind:
{
"calculator.add": [
[
1,
2
],
],
"calculator.multiplication": [
[
2,
2
]
],
"car.accelerate": [
[
"faster"
]
],
"car.decelerate": [
[
"slower"
]
]
}
Starting State
You can initialize the object with some starting state.
let resultBuilder = resultBuilderFactory({ author: 'RJK' });
resultBUilder.addDatum('some data', 8);
const result = resultBuilder.getResult();
The result looks like:
{
"author": "RJK",
"some data": 8
}