Sinon

While using BDD to optimize our current teams workflow there is all kinds of stuff we learn. BDD really drives better software. One of first lessons learned was to separate concerns and that Cucumber.js is certainly not limited to automated testing using selenium etc.

Currently we are building a dashboard to monitor the health of ad delivery. In the beginning we had a lot of business rules in our Polymer front-end however the BDD features we had described drove naturally towards a more back-end centered application.

Suddenly our development cycle was on steroids, because the only selenium tests we had were limited to some basic behavior when fed a data structure. Our ‘real’ features could now be executed on the API to validate the expected behavior.

The thing I would to share with you today is the hook we made for stubbing some our interfaces.

features/support/sinon_hooks.js

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

varsinon=require('sinon');

varstubs=[];

module.exports=function(){

this.Before(function(){

this.stub=function(object,func){

varstub=sinon.stub(object,func);

stubs.push(stub);

returnstub;

};

});

this.After(function(){

stubs.forEach(function(stub){

stub.restore();

});

});

};

Now in your step definitions you can stub the interface without worrying about restoring your stubs. All sinon.js stubs are wrapped into this small hooks.