forked from PerterPon/jm-spy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createSpyObj.coffee
50 lines (41 loc) · 1.09 KB
/
createSpyObj.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
expect = require 'expect.js'
jmspy = require 'jmspy'
Person = {}
Message =
sayHello: (id) ->
Person.getName id, (err, name) ->
if err
return console.log err
console.log "hello #{name}!"
sayAge: (id) ->
Person.getAge id, (err, age) ->
if err
return console.log err
console.log "my age is #{age}"
describe 'Message', () ->
spy1 = null
spy2 = null
before () ->
Person = jmspy.createSpyObj 'Person', ['getName', 'getAge']
spy1 = Person.getName
spy2 = Person.getAge
it 'sayHello success', () ->
spy1.and.callFake (id, cb) ->
expect(id).to.be 1
cb null, 'rose'
Message.sayHello 1
it 'sayHello fail', () ->
spy1.and.callFake (id, cb) ->
expect(id).to.be null
cb new Error 'sayHello id is null'
Message.sayHello null
it 'sayAge success', () ->
spy2.and.callFake (id, cb) ->
expect(id).to.be 1
cb null, 18
Message.sayAge 1
it 'sayAge fail', () ->
spy2.and.callFake (id, cb) ->
expect(id).to.be null
cb new Error 'sayAge id is null'
Message.sayAge null