test.js
3.51 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict'
var assert = require('assert')
var stubs = require('./')
var tess = require('tess')
tess('init', function(it) {
it('is a function', function() {
assert.equal(typeof stubs, 'function')
})
it('throws without obj || method', function() {
assert.throws(function() {
stubs()
}, /must provide/)
assert.throws(function() {
stubs({})
}, /must provide/)
})
})
tess('stubs', function(it) {
it('stubs a method with a noop', function() {
var originalCalled = false
var obj = {}
obj.method = function() {
originalCalled = true
}
stubs(obj, 'method')
obj.method()
assert(!originalCalled)
})
it('accepts an override', function() {
var originalCalled = false
var obj = {}
obj.method = function() {
originalCalled = true
}
var replacementCalled = false
stubs(obj, 'method', function() {
replacementCalled = true
})
obj.method()
assert(!originalCalled)
assert(replacementCalled)
})
it('returns value of overridden method call', function() {
var overriddenUniqueVal = {}
var obj = {}
obj.method = function() {}
stubs(obj, 'method', { callthrough: true }, function() {
return overriddenUniqueVal
})
assert.strictEqual(obj.method(), overriddenUniqueVal)
})
it('calls through to original method', function() {
var originalCalled = false
var obj = {}
obj.method = function() {
originalCalled = true
}
var replacementCalled = false
stubs(obj, 'method', { callthrough: true }, function() {
replacementCalled = true
})
obj.method()
assert(originalCalled)
assert(replacementCalled)
})
it('returns value of original method call', function() {
var uniqueVal = Date.now()
var obj = {}
obj.method = function() {
return uniqueVal
}
stubs(obj, 'method', { callthrough: true }, function() {})
assert.equal(obj.method(), uniqueVal)
})
it('returns calls override and returns original value', function() {
var uniqueVal = {}
var overrideWasCalled = false
var obj = {}
obj.method = function() {
return uniqueVal
}
stubs(obj, 'method', { callthrough: true }, function() {
// does not return anything
overrideWasCalled = true
})
assert.strictEqual(obj.method(), uniqueVal)
assert.strictEqual(overrideWasCalled, true)
})
it('returns value of overridden method call', function() {
var uniqueVal = {}
var overriddenUniqueVal = {}
var obj = {}
obj.method = function() {
return uniqueVal
}
stubs(obj, 'method', { callthrough: true }, function() {
return overriddenUniqueVal
})
assert.strictEqual(obj.method(), overriddenUniqueVal)
})
it('stops calling stub after n calls', function() {
var timesToCall = 5
var timesCalled = 0
var obj = {}
obj.method = function() {
assert.equal(timesCalled, timesToCall)
}
stubs(obj, 'method', { calls: timesToCall }, function() {
timesCalled++
})
obj.method() // 1 (stub)
obj.method() // 2 (stub)
obj.method() // 3 (stub)
obj.method() // 4 (stub)
obj.method() // 5 (stub)
obj.method() // 6 (original)
})
it('calls stub in original context of obj', function() {
var secret = 'brownies'
function Class() {
this.method = function() {}
this.secret = secret
}
var cl = new Class()
stubs(cl, 'method', function() {
assert.equal(this.secret, secret)
})
cl.method()
})
})