blob: e5d4a3bc2227624741dff2853413487299adc279 [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
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 Scandoloe3ed0162016-12-01 10:09:12 -080019(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})();