blob: abedb2950deb8da89ea312f309fb05c86e5c5be5 [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(function () {
2 'use strict';
3
4 const sinon = require('sinon');
5
6 let stubCache = {};
7
8 exports.makeStub = (name, target, method, cb) => {
9
10 let methodStub, prototypeStub;
11
12 function SuperAgentStub(end) {
13 this.end = end;
14 this.set = sinon.stub().returns(this);
15 this.send = sinon.stub().returns(this);
16 return this;
17 }
18
19 beforeEach(() => {
20 methodStub = sinon.stub(target, method);
21
22 prototypeStub = new SuperAgentStub(cb);
23
24 methodStub.returns(prototypeStub);
25
26 // cache stub (for use in tests)
27 stubCache[name] = {
28 set: prototypeStub.set,
29 send: prototypeStub.send
30 };
31 stubCache[name][method] = methodStub
32 });
33
34 afterEach(() => {
35 target[method].restore();
36 });
37
38 };
39
40 exports.getStub = name => stubCache[name];
41
42})();