-
Notifications
You must be signed in to change notification settings - Fork 12
/
mocha.test.js
148 lines (105 loc) · 3.74 KB
/
mocha.test.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict'
/*global describe beforeEach afterEach it*/
var assert = require('assert')
var simple = require('../index')
var AClass = require('./support/moduleA').AClass
describe('moduleA', function () {
afterEach(function () {
simple.restore()
})
describe('an instance of AClass', function () {
var myAClass
var config
beforeEach(function () {
config = {
someValue: Infinity
}
myAClass = new AClass(config)
})
describe('callingYou()', function () {
beforeEach(function () {
simple.mock(myAClass, 'callingMeSync')
simple.mock(myAClass, 'callingMeAsync')
})
it('can return if callingMeSync', function (done) {
myAClass.callingMeSync.returnWith(true)
myAClass.callingYou(1000, function (err, value) {
if (err) return done(err)
assert.equal(value, 1000)
done()
})
})
it('will return errors from callingMeAsync', function (done) {
var mockError = new Error()
myAClass.callingMeSync.returnWith(false)
myAClass.callingMeAsync.callbackWith(mockError)
myAClass.callingYou(1000, function (err, value) {
assert.equal(myAClass.callingMeSync.callCount, 1)
assert.equal(myAClass.callingMeAsync.callCount, 1)
assert.equal(myAClass.callingMeAsync.lastCall.arg, 2000)
assert.equal(err, mockError)
done()
})
})
it('will return if callingMeAsync', function (done) {
myAClass.callingMeSync.returnWith(false)
myAClass.callingMeAsync.callbackWith(null, true)
myAClass.callingYou(1000, function (err, value) {
if (err) return done(err)
assert.equal(myAClass.callingMeSync.callCount, 1)
assert.equal(myAClass.callingMeAsync.callCount, 1)
assert.equal(myAClass.callingMeAsync.lastCall.arg, 2000)
assert.equal(value, 2000)
done()
})
})
it('will return random number if neither callingMeSync or callingMeAsync', function (done) {
myAClass.callingMeSync.returnWith(false)
myAClass.callingMeAsync.callbackWith(null, false)
simple.mock(Math, 'random').returnWith(0.5)
myAClass.callingYou(1000, function (err, value) {
if (err) return done(err)
assert.equal(myAClass.callingMeSync.callCount, 1)
assert.equal(myAClass.callingMeAsync.callCount, 1)
assert.equal(myAClass.callingMeAsync.lastCall.arg, 2000)
assert.equal(value, 0.5)
done()
})
})
})
describe('callingMeSync()', function () {
it('can return false', function () {
simple.mock(config, 'someValue', 500)
var returned = myAClass.callingMeSync(501)
assert.equal(returned, false)
})
it('can return true', function () {
simple.mock(config, 'someValue', 501)
var returned = myAClass.callingMeSync(500)
assert.equal(returned, true)
})
})
describe('callingMeASync()', function () {
it('can return false', function () {
simple.mock(config, 'someValue', 500)
var cb = simple.mock()
myAClass.callingMeAsync(501, cb)
setImmediate(function () {
assert.equal(cb.callCount, 1)
assert.equal(cb.lastCall.args[0], null)
assert.equal(cb.lastCall.args[1], false)
})
})
it('can return true', function () {
simple.mock(config, 'someValue', 501)
var cb = simple.mock()
myAClass.callingMeAsync(500, cb)
setImmediate(function () {
assert.equal(cb.callCount, 1)
assert.equal(cb.lastCall.args[0], null)
assert.equal(cb.lastCall.args[1], true)
})
})
})
})
})