jasmine-core 2.99.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jasmine-core/boot.js +7 -6
- data/lib/jasmine-core/boot/boot.js +7 -6
- data/lib/jasmine-core/boot/node_boot.js +0 -3
- data/lib/jasmine-core/jasmine-html.js +228 -137
- data/lib/jasmine-core/jasmine.css +11 -5
- data/lib/jasmine-core/jasmine.js +1046 -608
- data/lib/jasmine-core/node_boot.js +0 -3
- data/lib/jasmine-core/spec/core/CallTrackerSpec.js +130 -0
- data/lib/jasmine-core/spec/core/ClearStackSpec.js +137 -0
- data/lib/jasmine-core/spec/core/ClockSpec.js +710 -0
- data/lib/jasmine-core/spec/core/DelayedFunctionSchedulerSpec.js +286 -0
- data/lib/jasmine-core/spec/core/EnvSpec.js +200 -0
- data/lib/jasmine-core/spec/core/ExceptionFormatterSpec.js +120 -0
- data/lib/jasmine-core/spec/core/ExceptionsSpec.js +46 -0
- data/lib/jasmine-core/spec/core/ExpectationResultSpec.js +61 -0
- data/lib/jasmine-core/spec/core/ExpectationSpec.js +434 -0
- data/lib/jasmine-core/spec/core/GlobalErrorsSpec.js +110 -0
- data/lib/jasmine-core/spec/core/JsApiReporterSpec.js +259 -0
- data/lib/jasmine-core/spec/core/MockDateSpec.js +200 -0
- data/lib/jasmine-core/spec/core/PrettyPrintSpec.js +332 -0
- data/lib/jasmine-core/spec/core/QueueRunnerSpec.js +670 -0
- data/lib/jasmine-core/spec/core/ReportDispatcherSpec.js +140 -0
- data/lib/jasmine-core/spec/core/SpecSpec.js +407 -0
- data/lib/jasmine-core/spec/core/SpyRegistrySpec.js +364 -0
- data/lib/jasmine-core/spec/core/SpySpec.js +177 -0
- data/lib/jasmine-core/spec/core/SpyStrategySpec.js +202 -0
- data/lib/jasmine-core/spec/core/StackTraceSpec.js +166 -0
- data/lib/jasmine-core/spec/core/SuiteSpec.js +123 -0
- data/lib/jasmine-core/spec/core/TimerSpec.js +31 -0
- data/lib/jasmine-core/spec/core/TreeProcessorSpec.js +794 -0
- data/lib/jasmine-core/spec/core/UserContextSpec.js +54 -0
- data/lib/jasmine-core/spec/core/UtilSpec.js +105 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/AnySpec.js +91 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/AnythingSpec.js +76 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ArrayContainingSpec.js +52 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ArrayWithExactContentsSpec.js +47 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/ObjectContainingSpec.js +99 -0
- data/lib/jasmine-core/spec/core/asymmetric_equality/StringMatchingSpec.js +27 -0
- data/lib/jasmine-core/spec/core/formatErrorMsgSpec.js +13 -0
- data/lib/jasmine-core/spec/core/integration/CustomMatchersSpec.js +200 -0
- data/lib/jasmine-core/spec/core/integration/CustomSpyStrategiesSpec.js +138 -0
- data/lib/jasmine-core/spec/core/integration/EnvSpec.js +2344 -0
- data/lib/jasmine-core/spec/core/integration/SpecRunningSpec.js +976 -0
- data/lib/jasmine-core/spec/core/matchers/DiffBuilderSpec.js +47 -0
- data/lib/jasmine-core/spec/core/matchers/NullDiffBuilderSpec.js +13 -0
- data/lib/jasmine-core/spec/core/matchers/ObjectPathSpec.js +43 -0
- data/lib/jasmine-core/spec/core/matchers/matchersUtilSpec.js +645 -0
- data/lib/jasmine-core/spec/core/matchers/nothingSpec.js +8 -0
- data/lib/jasmine-core/spec/core/matchers/toBeCloseToSpec.js +93 -0
- data/lib/jasmine-core/spec/core/matchers/toBeDefinedSpec.js +18 -0
- data/lib/jasmine-core/spec/core/matchers/toBeFalsySpec.js +38 -0
- data/lib/jasmine-core/spec/core/matchers/toBeGreaterThanOrEqualSpec.js +29 -0
- data/lib/jasmine-core/spec/core/matchers/toBeGreaterThanSpec.js +20 -0
- data/lib/jasmine-core/spec/core/matchers/toBeLessThanOrEqualSpec.js +29 -0
- data/lib/jasmine-core/spec/core/matchers/toBeLessThanSpec.js +20 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNaNSpec.js +37 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNegativeInfinitySpec.js +31 -0
- data/lib/jasmine-core/spec/core/matchers/toBeNullSpec.js +17 -0
- data/lib/jasmine-core/spec/core/matchers/toBePositiveInfinitySpec.js +31 -0
- data/lib/jasmine-core/spec/core/matchers/toBeSpec.js +17 -0
- data/lib/jasmine-core/spec/core/matchers/toBeTruthySpec.js +38 -0
- data/lib/jasmine-core/spec/core/matchers/toBeUndefinedSpec.js +18 -0
- data/lib/jasmine-core/spec/core/matchers/toContainSpec.js +26 -0
- data/lib/jasmine-core/spec/core/matchers/toEqualSpec.js +785 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledBeforeSpec.js +99 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledSpec.js +47 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledTimesSpec.js +86 -0
- data/lib/jasmine-core/spec/core/matchers/toHaveBeenCalledWithSpec.js +67 -0
- data/lib/jasmine-core/spec/core/matchers/toMatchSpec.js +43 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowErrorSpec.js +315 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowMatchingSpec.js +73 -0
- data/lib/jasmine-core/spec/core/matchers/toThrowSpec.js +100 -0
- data/lib/jasmine-core/spec/helpers/BrowserFlags.js +15 -0
- data/lib/jasmine-core/spec/helpers/asyncAwait.js +27 -0
- data/lib/jasmine-core/spec/helpers/checkForMap.js +37 -0
- data/lib/jasmine-core/spec/helpers/checkForSet.js +41 -0
- data/lib/jasmine-core/spec/helpers/checkForSymbol.js +28 -0
- data/lib/jasmine-core/spec/helpers/checkForTypedArrays.js +20 -0
- data/lib/jasmine-core/spec/helpers/defineJasmineUnderTest.js +6 -0
- data/lib/jasmine-core/spec/helpers/integrationMatchers.js +43 -0
- data/lib/jasmine-core/spec/helpers/nodeDefineJasmineUnderTest.js +30 -0
- data/lib/jasmine-core/spec/html/HtmlReporterSpec.js +1261 -0
- data/lib/jasmine-core/spec/html/HtmlSpecFilterSpec.js +18 -0
- data/lib/jasmine-core/spec/html/MatchersHtmlSpec.js +37 -0
- data/lib/jasmine-core/spec/html/PrettyPrintHtmlSpec.js +27 -0
- data/lib/jasmine-core/spec/html/QueryStringSpec.js +72 -0
- data/lib/jasmine-core/spec/html/ResultsNodeSpec.js +62 -0
- data/lib/jasmine-core/spec/html/SpyRegistryHtmlSpec.js +39 -0
- data/lib/jasmine-core/spec/html/matchers/toHaveClassSpec.js +48 -0
- data/lib/jasmine-core/spec/npmPackage/npmPackageSpec.js +101 -0
- data/lib/jasmine-core/spec/performance/large_object_test.js +36 -0
- data/lib/jasmine-core/spec/performance/performance_test.js +10 -0
- data/lib/jasmine-core/version.rb +1 -1
- metadata +88 -4
- data/lib/console/console.js +0 -190
@@ -0,0 +1,364 @@
|
|
1
|
+
describe("SpyRegistry", function() {
|
2
|
+
function createSpy(name, originalFn) {
|
3
|
+
return jasmineUnderTest.Spy(name, originalFn);
|
4
|
+
}
|
5
|
+
|
6
|
+
describe("#spyOn", function() {
|
7
|
+
it("checks for the existence of the object", function() {
|
8
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy});
|
9
|
+
expect(function() {
|
10
|
+
spyRegistry.spyOn(void 0, 'pants');
|
11
|
+
}).toThrowError(/could not find an object/);
|
12
|
+
});
|
13
|
+
|
14
|
+
it("checks that a method name was passed", function() {
|
15
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
16
|
+
subject = {};
|
17
|
+
|
18
|
+
expect(function() {
|
19
|
+
spyRegistry.spyOn(subject);
|
20
|
+
}).toThrowError(/No method name supplied/);
|
21
|
+
});
|
22
|
+
|
23
|
+
it("checks that the object is not `null`", function() {
|
24
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry();
|
25
|
+
expect(function() {
|
26
|
+
spyRegistry.spyOn(null, 'pants');
|
27
|
+
}).toThrowError(/could not find an object/);
|
28
|
+
});
|
29
|
+
|
30
|
+
it("checks that the method name is not `null`", function() {
|
31
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
32
|
+
subject = {};
|
33
|
+
|
34
|
+
expect(function() {
|
35
|
+
spyRegistry.spyOn(subject, null);
|
36
|
+
}).toThrowError(/No method name supplied/);
|
37
|
+
});
|
38
|
+
|
39
|
+
it("checks for the existence of the method", function() {
|
40
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
41
|
+
subject = {};
|
42
|
+
|
43
|
+
expect(function() {
|
44
|
+
spyRegistry.spyOn(subject, 'pants');
|
45
|
+
}).toThrowError(/method does not exist/);
|
46
|
+
});
|
47
|
+
|
48
|
+
it("checks if it has already been spied upon", function() {
|
49
|
+
var spies = [],
|
50
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
51
|
+
currentSpies: function() { return spies; },
|
52
|
+
createSpy: createSpy
|
53
|
+
}),
|
54
|
+
subject = { spiedFunc: function() {} };
|
55
|
+
|
56
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
57
|
+
|
58
|
+
expect(function() {
|
59
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
60
|
+
}).toThrowError(/has already been spied upon/);
|
61
|
+
});
|
62
|
+
|
63
|
+
it("checks if it can be spied upon", function() {
|
64
|
+
var scope = {};
|
65
|
+
|
66
|
+
function myFunc() {
|
67
|
+
return 1;
|
68
|
+
}
|
69
|
+
|
70
|
+
Object.defineProperty(scope, 'myFunc', {
|
71
|
+
get: function() {
|
72
|
+
return myFunc;
|
73
|
+
}
|
74
|
+
});
|
75
|
+
|
76
|
+
var spies = [],
|
77
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({currentSpies: function() { return spies; }}),
|
78
|
+
subject = { spiedFunc: scope.myFunc };
|
79
|
+
|
80
|
+
expect(function() {
|
81
|
+
spyRegistry.spyOn(scope, 'myFunc');
|
82
|
+
}).toThrowError(/is not declared writable or has no setter/);
|
83
|
+
|
84
|
+
expect(function() {
|
85
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
86
|
+
}).not.toThrowError(/is not declared writable or has no setter/);
|
87
|
+
});
|
88
|
+
|
89
|
+
it("overrides the method on the object and returns the spy", function() {
|
90
|
+
var originalFunctionWasCalled = false,
|
91
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
92
|
+
subject = { spiedFunc: function() { originalFunctionWasCalled = true; } };
|
93
|
+
|
94
|
+
var spy = spyRegistry.spyOn(subject, 'spiedFunc');
|
95
|
+
|
96
|
+
expect(subject.spiedFunc).toEqual(spy);
|
97
|
+
});
|
98
|
+
});
|
99
|
+
|
100
|
+
describe("#spyOnProperty", function() {
|
101
|
+
it("checks for the existence of the object", function() {
|
102
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry();
|
103
|
+
expect(function() {
|
104
|
+
spyRegistry.spyOnProperty(void 0, 'pants');
|
105
|
+
}).toThrowError(/could not find an object/);
|
106
|
+
});
|
107
|
+
|
108
|
+
it("checks that a property name was passed", function() {
|
109
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
110
|
+
subject = {};
|
111
|
+
|
112
|
+
expect(function() {
|
113
|
+
spyRegistry.spyOnProperty(subject);
|
114
|
+
}).toThrowError(/No property name supplied/);
|
115
|
+
});
|
116
|
+
|
117
|
+
it("checks for the existence of the method", function() {
|
118
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
119
|
+
subject = {};
|
120
|
+
|
121
|
+
expect(function() {
|
122
|
+
spyRegistry.spyOnProperty(subject, 'pants');
|
123
|
+
}).toThrowError(/property does not exist/);
|
124
|
+
});
|
125
|
+
|
126
|
+
it("checks for the existence of access type", function() {
|
127
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry(),
|
128
|
+
subject = {};
|
129
|
+
|
130
|
+
Object.defineProperty(subject, 'pants', {
|
131
|
+
get: function() { return 1; },
|
132
|
+
configurable: true
|
133
|
+
});
|
134
|
+
|
135
|
+
expect(function() {
|
136
|
+
spyRegistry.spyOnProperty(subject, 'pants', 'set');
|
137
|
+
}).toThrowError(/does not have access type/);
|
138
|
+
});
|
139
|
+
|
140
|
+
it("checks if it has already been spied upon", function() {
|
141
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
142
|
+
subject = {};
|
143
|
+
|
144
|
+
Object.defineProperty(subject, 'spiedProp', {
|
145
|
+
get: function() { return 1; },
|
146
|
+
configurable: true
|
147
|
+
});
|
148
|
+
|
149
|
+
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
150
|
+
|
151
|
+
expect(function() {
|
152
|
+
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
153
|
+
}).toThrowError(/has already been spied upon/);
|
154
|
+
});
|
155
|
+
|
156
|
+
it("checks if it can be spied upon", function() {
|
157
|
+
var subject = {};
|
158
|
+
|
159
|
+
Object.defineProperty(subject, 'myProp', {
|
160
|
+
get: function() {}
|
161
|
+
});
|
162
|
+
|
163
|
+
Object.defineProperty(subject, 'spiedProp', {
|
164
|
+
get: function() {},
|
165
|
+
configurable: true
|
166
|
+
});
|
167
|
+
|
168
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry();
|
169
|
+
|
170
|
+
expect(function() {
|
171
|
+
spyRegistry.spyOnProperty(subject, 'myProp');
|
172
|
+
}).toThrowError(/is not declared configurable/);
|
173
|
+
|
174
|
+
expect(function() {
|
175
|
+
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
176
|
+
}).not.toThrowError(/is not declared configurable/);
|
177
|
+
});
|
178
|
+
|
179
|
+
it("overrides the property getter on the object and returns the spy", function() {
|
180
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
181
|
+
subject = {},
|
182
|
+
returnValue = 1;
|
183
|
+
|
184
|
+
Object.defineProperty(subject, 'spiedProperty', {
|
185
|
+
get: function() { return returnValue; },
|
186
|
+
configurable: true
|
187
|
+
});
|
188
|
+
|
189
|
+
expect(subject.spiedProperty).toEqual(returnValue);
|
190
|
+
|
191
|
+
var spy = spyRegistry.spyOnProperty(subject, 'spiedProperty');
|
192
|
+
var getter = Object.getOwnPropertyDescriptor(subject, 'spiedProperty').get;
|
193
|
+
|
194
|
+
expect(getter).toEqual(spy);
|
195
|
+
expect(subject.spiedProperty).toBeUndefined();
|
196
|
+
});
|
197
|
+
|
198
|
+
it("overrides the property setter on the object and returns the spy", function() {
|
199
|
+
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
|
200
|
+
subject = {},
|
201
|
+
returnValue = 1;
|
202
|
+
|
203
|
+
Object.defineProperty(subject, 'spiedProperty', {
|
204
|
+
get: function() { return returnValue; },
|
205
|
+
set: function() {},
|
206
|
+
configurable: true
|
207
|
+
});
|
208
|
+
|
209
|
+
var spy = spyRegistry.spyOnProperty(subject, 'spiedProperty', 'set');
|
210
|
+
var setter = Object.getOwnPropertyDescriptor(subject, 'spiedProperty').set;
|
211
|
+
|
212
|
+
expect(subject.spiedProperty).toEqual(returnValue);
|
213
|
+
expect(setter).toEqual(spy);
|
214
|
+
});
|
215
|
+
});
|
216
|
+
|
217
|
+
describe("#clearSpies", function() {
|
218
|
+
it("restores the original functions on the spied-upon objects", function() {
|
219
|
+
var spies = [],
|
220
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
221
|
+
currentSpies: function() { return spies; },
|
222
|
+
createSpy: createSpy
|
223
|
+
}),
|
224
|
+
originalFunction = function() {},
|
225
|
+
subject = { spiedFunc: originalFunction };
|
226
|
+
|
227
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
228
|
+
spyRegistry.clearSpies();
|
229
|
+
|
230
|
+
expect(subject.spiedFunc).toBe(originalFunction);
|
231
|
+
});
|
232
|
+
|
233
|
+
it("restores the original functions, even when that spy has been replace and re-spied upon", function() {
|
234
|
+
var spies = [],
|
235
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
236
|
+
currentSpies: function() { return spies; },
|
237
|
+
createSpy: createSpy
|
238
|
+
}),
|
239
|
+
originalFunction = function() {},
|
240
|
+
subject = { spiedFunc: originalFunction };
|
241
|
+
|
242
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
243
|
+
|
244
|
+
// replace the original spy with some other function
|
245
|
+
subject.spiedFunc = function() {};
|
246
|
+
|
247
|
+
// spy on the function in that location again
|
248
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
249
|
+
|
250
|
+
spyRegistry.clearSpies();
|
251
|
+
|
252
|
+
expect(subject.spiedFunc).toBe(originalFunction);
|
253
|
+
});
|
254
|
+
|
255
|
+
it("does not add a property that the spied-upon object didn't originally have", function() {
|
256
|
+
var spies = [],
|
257
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
258
|
+
currentSpies: function() { return spies; },
|
259
|
+
createSpy: createSpy
|
260
|
+
}),
|
261
|
+
originalFunction = function() {},
|
262
|
+
subjectParent = {spiedFunc: originalFunction};
|
263
|
+
|
264
|
+
var subject = Object.create(subjectParent);
|
265
|
+
|
266
|
+
expect(subject.hasOwnProperty('spiedFunc')).toBe(false);
|
267
|
+
|
268
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
269
|
+
spyRegistry.clearSpies();
|
270
|
+
|
271
|
+
expect(subject.hasOwnProperty('spiedFunc')).toBe(false);
|
272
|
+
expect(subject.spiedFunc).toBe(originalFunction);
|
273
|
+
});
|
274
|
+
|
275
|
+
it("restores the original function when it\'s inherited and cannot be deleted", function() {
|
276
|
+
var spies = [],
|
277
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
278
|
+
currentSpies: function() { return spies; },
|
279
|
+
createSpy: createSpy
|
280
|
+
}),
|
281
|
+
originalFunction = function() {},
|
282
|
+
subjectParent = {spiedFunc: originalFunction};
|
283
|
+
|
284
|
+
var subject = Object.create(subjectParent);
|
285
|
+
|
286
|
+
spyRegistry.spyOn(subject, 'spiedFunc');
|
287
|
+
|
288
|
+
// simulate a spy that cannot be deleted
|
289
|
+
Object.defineProperty(subject, 'spiedFunc', {
|
290
|
+
configurable: false
|
291
|
+
});
|
292
|
+
|
293
|
+
spyRegistry.clearSpies();
|
294
|
+
|
295
|
+
expect(jasmineUnderTest.isSpy(subject.spiedFunc)).toBe(false);
|
296
|
+
});
|
297
|
+
|
298
|
+
it("restores window.onerror by overwriting, not deleting", function() {
|
299
|
+
function FakeWindow() {
|
300
|
+
}
|
301
|
+
FakeWindow.prototype.onerror = function() {};
|
302
|
+
|
303
|
+
var spies = [],
|
304
|
+
global = new FakeWindow(),
|
305
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
306
|
+
currentSpies: function() { return spies; },
|
307
|
+
createSpy: createSpy,
|
308
|
+
global: global
|
309
|
+
});
|
310
|
+
|
311
|
+
spyRegistry.spyOn(global, 'onerror');
|
312
|
+
spyRegistry.clearSpies();
|
313
|
+
expect(global.onerror).toBe(FakeWindow.prototype.onerror);
|
314
|
+
expect(global.hasOwnProperty('onerror')).toBe(true);
|
315
|
+
});
|
316
|
+
});
|
317
|
+
|
318
|
+
describe('spying on properties', function() {
|
319
|
+
it("restores the original properties on the spied-upon objects", function() {
|
320
|
+
var spies = [],
|
321
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
322
|
+
currentSpies: function() { return spies; },
|
323
|
+
createSpy: createSpy
|
324
|
+
}),
|
325
|
+
originalReturn = 1,
|
326
|
+
subject = {};
|
327
|
+
|
328
|
+
Object.defineProperty(subject, 'spiedProp', {
|
329
|
+
get: function() { return originalReturn; },
|
330
|
+
configurable: true
|
331
|
+
});
|
332
|
+
|
333
|
+
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
334
|
+
spyRegistry.clearSpies();
|
335
|
+
|
336
|
+
expect(subject.spiedProp).toBe(originalReturn);
|
337
|
+
});
|
338
|
+
|
339
|
+
it("does not add a property that the spied-upon object didn't originally have", function() {
|
340
|
+
var spies = [],
|
341
|
+
spyRegistry = new jasmineUnderTest.SpyRegistry({
|
342
|
+
currentSpies: function() { return spies; },
|
343
|
+
createSpy: createSpy
|
344
|
+
}),
|
345
|
+
originalReturn = 1,
|
346
|
+
subjectParent = {};
|
347
|
+
|
348
|
+
Object.defineProperty(subjectParent, 'spiedProp', {
|
349
|
+
get: function() { return originalReturn; },
|
350
|
+
configurable: true
|
351
|
+
});
|
352
|
+
|
353
|
+
var subject = Object.create(subjectParent);
|
354
|
+
|
355
|
+
expect(subject.hasOwnProperty('spiedProp')).toBe(false);
|
356
|
+
|
357
|
+
spyRegistry.spyOnProperty(subject, 'spiedProp');
|
358
|
+
spyRegistry.clearSpies();
|
359
|
+
|
360
|
+
expect(subject.hasOwnProperty('spiedProp')).toBe(false);
|
361
|
+
expect(subject.spiedProp).toBe(originalReturn);
|
362
|
+
});
|
363
|
+
});
|
364
|
+
});
|
@@ -0,0 +1,177 @@
|
|
1
|
+
describe('Spies', function () {
|
2
|
+
var env;
|
3
|
+
|
4
|
+
beforeEach(function() {
|
5
|
+
env = new jasmineUnderTest.Env();
|
6
|
+
});
|
7
|
+
|
8
|
+
describe("createSpy", function() {
|
9
|
+
var TestClass;
|
10
|
+
|
11
|
+
beforeEach(function() {
|
12
|
+
TestClass = function() {};
|
13
|
+
TestClass.prototype.someFunction = function() {};
|
14
|
+
TestClass.prototype.someFunction.bob = "test";
|
15
|
+
});
|
16
|
+
|
17
|
+
it("preserves the properties of the spied function", function() {
|
18
|
+
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
19
|
+
|
20
|
+
expect(spy.bob).toEqual("test");
|
21
|
+
});
|
22
|
+
|
23
|
+
it("warns the user that we intend to overwrite an existing property", function() {
|
24
|
+
TestClass.prototype.someFunction.and = "turkey";
|
25
|
+
|
26
|
+
expect(function() {
|
27
|
+
env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
28
|
+
}).toThrowError("Jasmine spies would overwrite the 'and' and 'calls' properties on the object being spied upon");
|
29
|
+
});
|
30
|
+
|
31
|
+
it("adds a spyStrategy and callTracker to the spy", function() {
|
32
|
+
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
33
|
+
|
34
|
+
expect(spy.and).toEqual(jasmine.any(jasmineUnderTest.SpyStrategy));
|
35
|
+
expect(spy.calls).toEqual(jasmine.any(jasmineUnderTest.CallTracker));
|
36
|
+
});
|
37
|
+
|
38
|
+
it("tracks the argument of calls", function () {
|
39
|
+
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
40
|
+
var trackSpy = spyOn(spy.calls, "track");
|
41
|
+
|
42
|
+
spy("arg");
|
43
|
+
|
44
|
+
expect(trackSpy.calls.mostRecent().args[0].args).toEqual(["arg"]);
|
45
|
+
});
|
46
|
+
|
47
|
+
it("tracks the context of calls", function () {
|
48
|
+
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
49
|
+
var trackSpy = spyOn(spy.calls, "track");
|
50
|
+
|
51
|
+
var contextObject = { spyMethod: spy };
|
52
|
+
contextObject.spyMethod();
|
53
|
+
|
54
|
+
expect(trackSpy.calls.mostRecent().args[0].object).toEqual(contextObject);
|
55
|
+
});
|
56
|
+
|
57
|
+
it("tracks the return value of calls", function () {
|
58
|
+
var spy = env.createSpy(TestClass.prototype, TestClass.prototype.someFunction);
|
59
|
+
var trackSpy = spyOn(spy.calls, "track");
|
60
|
+
|
61
|
+
spy.and.returnValue("return value");
|
62
|
+
spy();
|
63
|
+
|
64
|
+
expect(trackSpy.calls.mostRecent().args[0].returnValue).toEqual("return value");
|
65
|
+
});
|
66
|
+
|
67
|
+
it("preserves arity of original function", function () {
|
68
|
+
var functions = [
|
69
|
+
function nullary () {},
|
70
|
+
function unary (arg) {},
|
71
|
+
function binary (arg1, arg2) {},
|
72
|
+
function ternary (arg1, arg2, arg3) {},
|
73
|
+
function quaternary (arg1, arg2, arg3, arg4) {},
|
74
|
+
function quinary (arg1, arg2, arg3, arg4, arg5) {},
|
75
|
+
function senary (arg1, arg2, arg3, arg4, arg5, arg6) {}
|
76
|
+
];
|
77
|
+
|
78
|
+
for (var arity = 0; arity < functions.length; arity++) {
|
79
|
+
var someFunction = functions[arity],
|
80
|
+
spy = env.createSpy(someFunction.name, someFunction);
|
81
|
+
|
82
|
+
expect(spy.length).toEqual(arity);
|
83
|
+
}
|
84
|
+
});
|
85
|
+
});
|
86
|
+
|
87
|
+
describe("createSpyObj", function() {
|
88
|
+
it("should create an object with spy methods and corresponding return values when you call jasmine.createSpyObj() with an object", function () {
|
89
|
+
var spyObj = env.createSpyObj('BaseName', {'method1': 42, 'method2': 'special sauce' });
|
90
|
+
|
91
|
+
expect(spyObj.method1()).toEqual(42);
|
92
|
+
expect(spyObj.method1.and.identity).toEqual('BaseName.method1');
|
93
|
+
|
94
|
+
expect(spyObj.method2()).toEqual('special sauce');
|
95
|
+
expect(spyObj.method2.and.identity).toEqual('BaseName.method2');
|
96
|
+
});
|
97
|
+
|
98
|
+
|
99
|
+
it("should create an object with a bunch of spy methods when you call jasmine.createSpyObj()", function() {
|
100
|
+
var spyObj = env.createSpyObj('BaseName', ['method1', 'method2']);
|
101
|
+
|
102
|
+
expect(spyObj).toEqual({ method1: jasmine.any(Function), method2: jasmine.any(Function)});
|
103
|
+
expect(spyObj.method1.and.identity).toEqual('BaseName.method1');
|
104
|
+
expect(spyObj.method2.and.identity).toEqual('BaseName.method2');
|
105
|
+
});
|
106
|
+
|
107
|
+
it("should allow you to omit the baseName", function() {
|
108
|
+
var spyObj = env.createSpyObj(['method1', 'method2']);
|
109
|
+
|
110
|
+
expect(spyObj).toEqual({ method1: jasmine.any(Function), method2: jasmine.any(Function)});
|
111
|
+
expect(spyObj.method1.and.identity).toEqual('unknown.method1');
|
112
|
+
expect(spyObj.method2.and.identity).toEqual('unknown.method2');
|
113
|
+
});
|
114
|
+
|
115
|
+
it("should throw if you do not pass an array or object argument", function() {
|
116
|
+
expect(function() {
|
117
|
+
env.createSpyObj('BaseName');
|
118
|
+
}).toThrow("createSpyObj requires a non-empty array or object of method names to create spies for");
|
119
|
+
});
|
120
|
+
|
121
|
+
it("should throw if you pass an empty array argument", function() {
|
122
|
+
expect(function() {
|
123
|
+
env.createSpyObj('BaseName', []);
|
124
|
+
}).toThrow("createSpyObj requires a non-empty array or object of method names to create spies for");
|
125
|
+
});
|
126
|
+
|
127
|
+
it("should throw if you pass an empty object argument", function() {
|
128
|
+
expect(function() {
|
129
|
+
env.createSpyObj('BaseName', {});
|
130
|
+
}).toThrow("createSpyObj requires a non-empty array or object of method names to create spies for");
|
131
|
+
});
|
132
|
+
});
|
133
|
+
|
134
|
+
it("can use different strategies for different arguments", function() {
|
135
|
+
var spy = env.createSpy('foo');
|
136
|
+
spy.and.returnValue(42);
|
137
|
+
spy.withArgs('baz', 'grault').and.returnValue(-1);
|
138
|
+
spy.withArgs('thud').and.returnValue('bob');
|
139
|
+
|
140
|
+
expect(spy('foo')).toEqual(42);
|
141
|
+
expect(spy('baz', 'grault')).toEqual(-1);
|
142
|
+
expect(spy('thud')).toEqual('bob');
|
143
|
+
expect(spy('baz', 'grault', 'waldo')).toEqual(42);
|
144
|
+
});
|
145
|
+
|
146
|
+
it("uses custom equality testers when selecting a strategy", function() {
|
147
|
+
var spy = env.createSpy('foo');
|
148
|
+
spy.and.returnValue(42);
|
149
|
+
spy.withArgs(jasmineUnderTest.any(String)).and.returnValue(-1);
|
150
|
+
|
151
|
+
expect(spy('foo')).toEqual(-1);
|
152
|
+
expect(spy({})).toEqual(42);
|
153
|
+
});
|
154
|
+
|
155
|
+
it("can reconfigure an argument-specific strategy", function() {
|
156
|
+
var spy = env.createSpy('foo');
|
157
|
+
spy.withArgs('foo').and.returnValue(42);
|
158
|
+
spy.withArgs('foo').and.returnValue(17);
|
159
|
+
expect(spy('foo')).toEqual(17);
|
160
|
+
});
|
161
|
+
|
162
|
+
describe("When withArgs is used without a base strategy", function() {
|
163
|
+
it("uses the matching strategy", function() {
|
164
|
+
var spy = env.createSpy('foo');
|
165
|
+
spy.withArgs('baz').and.returnValue(-1);
|
166
|
+
|
167
|
+
expect(spy('baz')).toEqual(-1);
|
168
|
+
});
|
169
|
+
|
170
|
+
it("throws if the args don't match", function() {
|
171
|
+
var spy = env.createSpy('foo');
|
172
|
+
spy.withArgs('bar').and.returnValue(-1);
|
173
|
+
|
174
|
+
expect(function() { spy('baz', {qux: 42}); }).toThrowError('Spy \'foo\' receieved a call with arguments [ \'baz\', Object({ qux: 42 }) ] but all configured strategies specify other arguments.');
|
175
|
+
});
|
176
|
+
});
|
177
|
+
});
|