UNPKG

755 Btext/coffeescriptView Raw
1sinon = require "sinon"
2_ = require "lodash"
3sandbox = []
4
5register = (type, obj, method, func) ->
6 item = null
7
8 try
9 if type == "stub" && !_.isEmpty(func)
10 item = sinon.stub(obj, method).callsFake(func)
11 else
12 item = sinon[type].apply(sinon, [ obj, method, func ])
13
14 sandbox.push item
15 catch e
16 if /already wrapped/i.test e.message
17 item = obj[method]
18 else throw e
19
20 item
21
22stub = (obj, method, func) ->
23 register 'stub', obj, method, func
24
25spy = (obj, method) ->
26 register 'spy', obj, method
27
28reset = ->
29 _.each sandbox, (stub_or_spy) ->
30 stub_or_spy.reset()
31
32restore = ->
33 _.each sandbox, (stub) ->
34 stub.restore() if stub.restore
35
36module.exports =
37 stub: stub
38 spy: spy
39 reset: reset
40 restore: restore