Matteo Scandolo | bd13aab | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | const sinon = require('sinon'); |
| 23 | |
| 24 | let stubCache = {}; |
| 25 | |
| 26 | exports.makeStub = (name, target, method, cb) => { |
| 27 | |
| 28 | let methodStub, prototypeStub; |
| 29 | |
| 30 | function SuperAgentStub(end) { |
| 31 | this.end = end; |
| 32 | this.set = sinon.stub().returns(this); |
| 33 | this.send = sinon.stub().returns(this); |
| 34 | return this; |
| 35 | } |
| 36 | |
| 37 | beforeEach(() => { |
| 38 | methodStub = sinon.stub(target, method); |
| 39 | |
| 40 | prototypeStub = new SuperAgentStub(cb); |
| 41 | |
| 42 | methodStub.returns(prototypeStub); |
| 43 | |
| 44 | // cache stub (for use in tests) |
| 45 | stubCache[name] = { |
| 46 | set: prototypeStub.set, |
| 47 | send: prototypeStub.send |
| 48 | }; |
| 49 | stubCache[name][method] = methodStub |
| 50 | }); |
| 51 | |
| 52 | afterEach(() => { |
| 53 | target[method].restore(); |
| 54 | }); |
| 55 | |
| 56 | }; |
| 57 | |
| 58 | exports.getStub = name => stubCache[name]; |
| 59 | |
| 60 | })(); |