Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (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 | })(); |