1 | # referee-sinon
|
2 |
|
3 | Sinon.JS and the referee assertion library in one package.
|
4 |
|
5 |
|
6 | ## Usage
|
7 |
|
8 | ```shell
|
9 | npm install @sinonjs/referee-sinon --save-dev
|
10 | ```
|
11 |
|
12 | Note that you don't need to install `@sinonjs/referee` or `sinon`.
|
13 |
|
14 | ```js
|
15 | const referee = require("@sinonjs/referee-sinon");
|
16 |
|
17 | const assert = referee.assert
|
18 | const refute = referee.refute
|
19 | const sinon = referee.sinon
|
20 | ```
|
21 |
|
22 | Or, [if you can make use][compat] of [destructuring assignments][mdn]:
|
23 |
|
24 | ```js
|
25 | const { assert, refute, sinon } = require("@sinonjs/referee-sinon");
|
26 | ```
|
27 |
|
28 | [compat]: http://kangax.github.io/compat-table/es6/#test-destructuring
|
29 | [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
|
30 |
|
31 | ## Sinon
|
32 |
|
33 | The exposed `sinon` object is the full Sinon.JS API as [documented on the Sinon.JS homepage](http://sinonjs.org).
|
34 |
|
35 | ## Assertions
|
36 |
|
37 | The descriptions are for `assert`, but the corresponding failure messages for `refute` are also mentioned. For refute the behaviour is exactly opposite.
|
38 |
|
39 | *Overview:*
|
40 |
|
41 | * [`called()`](#called)
|
42 | * [`callCount()`](#callcount)
|
43 | * [`callOrder()`](#callorder)
|
44 | * [`calledOnce()`](#calledonce)
|
45 | * [`calledTwice()`](#calledtwice)
|
46 | * [`calledThrice()`](#calledthrice)
|
47 | * [`calledOn()`](#calledon)
|
48 | * [`alwaysCalledOn()`](#alwayscalledon)
|
49 | * [`calledWith()`](#calledwith)
|
50 | * [`calledWithNew()`](#calledwithnew)
|
51 | * [`alwaysCalledWith()`](alwayscalledwith)
|
52 | * [`alwaysCalledWithNew()`](alwayscalledwithnew)
|
53 | * [`calledOnceWith()`](#calledoncewith)
|
54 | * [`calledWithExactly()`](#calledwithexactly)
|
55 | * [`alwaysCalledWithExactly()`](#alwayscalledwithexactly)
|
56 | * [`threw()`](#threw)
|
57 | * [`alwaysThrew()`](#alwaysthrew)
|
58 |
|
59 | ### `called()`
|
60 |
|
61 | ```js
|
62 | assert.called(spy)
|
63 | ```
|
64 |
|
65 | Fails if the `spy` has never been called.
|
66 |
|
67 | ```js
|
68 | var spy = this.spy();
|
69 |
|
70 | assert.called(spy); // Fails
|
71 |
|
72 | spy();
|
73 | assert.called(spy); // Passes
|
74 |
|
75 | spy();
|
76 | assert.called(spy); // Passes
|
77 | ```
|
78 |
|
79 | #### Messages
|
80 |
|
81 | ```js
|
82 | assert.called.message = "Expected ${0} to be called at least once but was never called";
|
83 | ```
|
84 |
|
85 | <dl>
|
86 | <dt>`${0}`:</dt>
|
87 | <dd>The spy</dd>
|
88 | </dl>
|
89 |
|
90 | ```js
|
91 | refute.called.message = "Expected ${0} to not be called but was called ${1}${2}";
|
92 | ```
|
93 |
|
94 |
|
95 | <dl>
|
96 | <dt>`${0}`:</dt>
|
97 | <dd>The spy</dd>
|
98 | <dt>`${1}`:</dt>
|
99 | <dd>The number of calls as a string. Ex: “two times”</dd>
|
100 | <dt>`${2}`:</dt>
|
101 | <dd>All calls formatted as a multi-line string</dd>
|
102 | </dl>
|
103 |
|
104 | ### `callCount()`
|
105 |
|
106 | ```js
|
107 | assert.callCount(spy, count)
|
108 | ```
|
109 |
|
110 | Fails if the `spy`'s `callCount` property is not exactly `count`
|
111 |
|
112 | ```js
|
113 | var spy = this.spy();
|
114 |
|
115 | assert.callCount(spy, 0); // Passes
|
116 | assert.callCount(spy, 1); // Fails
|
117 |
|
118 | spy();
|
119 | assert.callCount(spy, 0); // Fails
|
120 | assert.callCount(spy, 1); // Passes
|
121 | ```
|
122 |
|
123 | #### Messages
|
124 |
|
125 | ```js
|
126 | assert.called.message = "Expected ${spyObj} to be called exactly ${expectedTimes} times, but was called ${actualTimes}";
|
127 | refute.called.message = "Expected ${spyObj} to not be called exactly ${expectedTimes} times"
|
128 | ```
|
129 |
|
130 | <dl>
|
131 | <dt>`${spyObj}`:</dt>
|
132 | <dd>The spy</dd>
|
133 | <dt>`${expectedTimes}`:</dt>
|
134 | <dd>The expected number of calls</dd>
|
135 | <dt>`${actualTimes}`:</dt>
|
136 | <dd>The actual number of calls</dd>
|
137 | </dl>
|
138 |
|
139 |
|
140 | ### `callOrder()`
|
141 |
|
142 | ```js
|
143 | assert.callOrder(spy, spy2, ...)
|
144 | ```
|
145 |
|
146 | Fails if the spies were not called in the specified order.
|
147 |
|
148 | ```js
|
149 | var spy1 = this.spy();
|
150 | var spy2 = this.spy();
|
151 | var spy3 = this.spy();
|
152 |
|
153 | spy1();
|
154 | spy2();
|
155 | spy3();
|
156 |
|
157 | assert.callOrder(spy1, spy3, spy2); // Fails
|
158 | assert.callOrder(spy1, spy2, spy3); // Passes
|
159 | ```
|
160 |
|
161 | #### Messages
|
162 |
|
163 | ```js
|
164 | assert.callOrder.message = "Expected ${expected} to be called in order but were called as ${actual}";
|
165 | refute.callOrder.message = "Expected ${expected} not to be called in order";
|
166 | ```
|
167 |
|
168 | <dl>
|
169 | <dt>`${expected}`:</dt>
|
170 | <dd>A string representation of the expected call order</dd>
|
171 | <dt>`${actual}`:</dt>
|
172 | <dd>A string representation of the actual call order</dd>
|
173 | </dl>
|
174 |
|
175 |
|
176 | ### `calledOnce()`
|
177 |
|
178 | ```js
|
179 | assert.calledOnce(spy)
|
180 | ```
|
181 |
|
182 | Fails if the `spy` has never been called or if it was called more than once.
|
183 |
|
184 | ```js
|
185 | var spy = this.spy();
|
186 |
|
187 | assert.calledOnce(spy); // Fails
|
188 |
|
189 | spy();
|
190 | assert.calledOnce(spy); // Passes
|
191 |
|
192 | spy();
|
193 | assert.calledOnce(spy); // Fails
|
194 | ```
|
195 |
|
196 | #### Messages
|
197 |
|
198 | ```
|
199 | assert.calledOnce.message = "Expected ${0} to be called once but was called ${1}${2}";
|
200 | refute.calledOnce.message = "Expected ${0} to not be called exactly once${2}";
|
201 | ```
|
202 |
|
203 | <dl>
|
204 | <dt>`${0}`:</dt>
|
205 | <dd>The spy</dd>
|
206 | <dt>`${1}`:</dt>
|
207 | <dd>The number of calls, as a string. Ex: “two times”</dd>
|
208 | <dt>`${2}`:</dt>
|
209 | <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
|
210 | </dl>
|
211 |
|
212 | ### `calledTwice()`
|
213 |
|
214 | ```js
|
215 | assert.calledTwice(spy)
|
216 | ```
|
217 |
|
218 | Only passes if the `spy` was called exactly twice.
|
219 |
|
220 | ```js
|
221 | var spy = this.spy();
|
222 |
|
223 | assert.calledTwice(spy); // Fails
|
224 |
|
225 | spy();
|
226 | assert.calledTwice(spy); // Fails
|
227 |
|
228 | spy();
|
229 | assert.calledTwice(spy); // Passes
|
230 |
|
231 | spy();
|
232 | assert.calledTwice(spy); // Fails
|
233 | ```
|
234 |
|
235 | #### Messages
|
236 |
|
237 | ```js
|
238 | assert.calledTwice.message = "Expected ${0} to be called twice but was called ${1}${2}";
|
239 | refute.calledTwice.message = "Expected ${0} to not be called exactly twice${2}";
|
240 | ```
|
241 |
|
242 | <dl>
|
243 | <dt>`${0}`:</dt>
|
244 | <dd>The spy</dd>
|
245 | <dt>`${1}`:</dt>
|
246 | <dd>The number of calls, as a string. Ex: “two times”</dd>
|
247 | <dt>`${2}`:</dt>
|
248 | <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
|
249 | </dl>
|
250 |
|
251 | ### `calledThrice()`
|
252 |
|
253 | ```js
|
254 | assert.calledThrice(spy)
|
255 | ```
|
256 |
|
257 | Only passes if the `spy` has been called exactly three times.
|
258 |
|
259 | ```js
|
260 | var spy = this.spy();
|
261 |
|
262 | assert.calledThrice(spy); // Fails
|
263 |
|
264 | spy();
|
265 | assert.calledThrice(spy); // Fails
|
266 |
|
267 | spy();
|
268 | assert.calledThrice(spy); // Fails
|
269 |
|
270 | spy();
|
271 | assert.calledThrice(spy); // Passes
|
272 |
|
273 | spy();
|
274 | assert.calledThrice(spy); // Fails
|
275 | ```
|
276 |
|
277 | #### Messages
|
278 |
|
279 | ```js
|
280 | assert.calledThrice.message = "Expected ${0} to be called thrice but was called ${1}${2}";
|
281 | refute.calledThrice.message = "Expected ${0} to not be called exactly thrice${2}";
|
282 | ```
|
283 |
|
284 | <dl>
|
285 | <dt>`${0}`:</dt>
|
286 | <dd>The spy</dd>
|
287 | <dt>`${1}`:</dt>
|
288 | <dd>The number of calls, as a string. Ex: “two times”</dd>
|
289 | <dt>`${2}`:</dt>
|
290 | <dd>The call log. All calls as a string. Each line is one call and includes passed arguments, returned value and more</dd>
|
291 | </dl>
|
292 |
|
293 | ### `calledOn()`
|
294 |
|
295 | ```js
|
296 | assert.calledOn(spy, obj)
|
297 | ```
|
298 |
|
299 | Passes if the `spy` was called at least once with `obj` as its `this` value.
|
300 |
|
301 | ```js
|
302 | var spy = this.spy();
|
303 | var obj1 = {};
|
304 | var obj2 = {};
|
305 | var obj3 = {};
|
306 |
|
307 | spy.call(obj2);
|
308 | spy.call(obj3);
|
309 |
|
310 | assert.calledOn(spy, obj1); // Fails
|
311 | assert.calledOn(spy, obj2); // Passes
|
312 | assert.calledOn(spy, obj3); // Passes
|
313 | ```
|
314 |
|
315 | #### Messages
|
316 |
|
317 | ```js
|
318 | assert.calledOn.message = "Expected ${0} to be called with ${1} as this but was called on ${2}";
|
319 | refute.calledOn.message = "Expected ${0} not to be called with ${1} as this";
|
320 | ```
|
321 |
|
322 | <dl>
|
323 | <dt>`${0}`:</dt>
|
324 | <dd>The spy</dd>
|
325 | <dt>`${1}`:</dt>
|
326 | <dd>The object obj which is expected to have been this at least once</dd>
|
327 | <dt>`${2}`:</dt>
|
328 | <dd>List of objects which actually have been `this`</dd>
|
329 | </dl>
|
330 |
|
331 | ### `alwaysCalledOn()`
|
332 |
|
333 | ```js
|
334 | assert.alwaysCalledOn(spy, obj)
|
335 | ```
|
336 |
|
337 | Passes if the `spy` was always called with `obj` as its `this` value.
|
338 |
|
339 | ```js
|
340 | var spy1 = this.spy();
|
341 | var spy2 = this.spy();
|
342 | var obj1 = {};
|
343 | var obj2 = {};
|
344 |
|
345 | spy1.call(obj1);
|
346 | spy1.call(obj2);
|
347 |
|
348 | spy2.call(obj2);
|
349 | spy2.call(obj2);
|
350 |
|
351 | assert.alwaysCalledOn(spy1, obj1); // Fails
|
352 | assert.alwaysCalledOn(spy1, obj2); // Fails
|
353 | assert.alwaysCalledOn(spy2, obj1); // Fails
|
354 | assert.alwaysCalledOn(spy2, obj2); // Passes
|
355 | ```
|
356 |
|
357 | #### Messages
|
358 |
|
359 | ```js
|
360 | assert.alwaysCalledOn.message = "Expected ${0} to always be called with ${1} as this but was called on ${2}";
|
361 | refute.alwaysCalledOn.message = "Expected ${0} not to always be called with ${1} as this";
|
362 | ```
|
363 |
|
364 | <dl>
|
365 | <dt>`${0}`:</dt>
|
366 | <dd>The spy</dd>
|
367 | <dt>`${1}`:</dt>
|
368 | <dd>The object obj which is expected always to have been `this`</dd>
|
369 | <dt>`${2}`:</dt>
|
370 | <dd>List of objects which actually have been `this`</dd>
|
371 | </dl>
|
372 |
|
373 |
|
374 | ### `calledWith()`
|
375 |
|
376 | ```js
|
377 | assert.calledWith(spy, arg1, arg2, ...)
|
378 | ```
|
379 |
|
380 | Passes if the `spy` was called at least once with the specified arguments. Other arguments may have been passed after the specified ones.
|
381 |
|
382 | ```js
|
383 | var spy = this.spy();
|
384 | var arr = [1, 2, 3];
|
385 | spy(12);
|
386 | spy(42, 13);
|
387 | spy("Hey", arr, 2);
|
388 |
|
389 | assert.calledWith(spy, 12); // Passes
|
390 | assert.calledWith(spy, "Hey"); // Passes
|
391 | assert.calledWith(spy, "Hey", 12); // Fails
|
392 | assert.calledWith(spy, "Hey", arr); // Passes
|
393 | ```
|
394 |
|
395 | #### Messages
|
396 |
|
397 | ```js
|
398 | assert.calledWith.message = "Expected ${0} to be called with arguments ${1}${2}";
|
399 | refute.calledWith.message = "Expected ${0} not to be called with arguments ${1}${2}";
|
400 | ```
|
401 |
|
402 | <dl>
|
403 | <dt>`${0}`:</dt>
|
404 | <dd>The spy</dd>
|
405 | <dt>`${1}`:</dt>
|
406 | <dd>The expected arguments</dd>
|
407 | <dt>`${2}`:</dt>
|
408 | <dd>String representation of all calls</dd>
|
409 | </dl>
|
410 |
|
411 | ### `calledWithNew()`
|
412 |
|
413 | ```js
|
414 | assert.calledWithNew(spy)
|
415 | ```
|
416 |
|
417 | Fails if the `spy` has never called with `new`.
|
418 |
|
419 | ```js
|
420 | var spy = this.spy();
|
421 |
|
422 | assert.calledWithNew(spy); // Fails
|
423 |
|
424 | new spy();
|
425 | assert.calledWithNew(spy); // Passes
|
426 |
|
427 | spy();
|
428 | assert.calledWithNew(spy); // Passes
|
429 | ```
|
430 |
|
431 | #### Messages
|
432 |
|
433 | ```js
|
434 | assert.calledWithNew.message = "Expected ${spyObj} to be called with 'new' at least once but was never called with 'new'";
|
435 | refute.calledWithNew.message = "Expected ${spyObj} to not be called with 'new'";
|
436 | ```
|
437 |
|
438 | <dl>
|
439 | <dt>`${spyObj}`:</dt>
|
440 | <dd>The spy</dd>
|
441 | </dl>
|
442 |
|
443 |
|
444 | ### `alwaysCalledWith()`
|
445 |
|
446 | ```js
|
447 | assert.alwaysCalledWith(spy, arg1, arg2, ...)
|
448 | ```
|
449 |
|
450 | Passes if the `spy` was always called with the specified arguments. Other arguments may have been passed after the specified ones.
|
451 |
|
452 | ```js
|
453 | var spy = this.spy();
|
454 | var arr = [1, 2, 3];
|
455 | spy("Hey", arr, 12);
|
456 | spy("Hey", arr, 13);
|
457 |
|
458 | assert.alwaysCalledWith(spy, "Hey"); // Passes
|
459 | assert.alwaysCalledWith(spy, "Hey", arr); // Passes
|
460 | assert.alwaysCalledWith(spy, "Hey", arr, 12); // Fails
|
461 | ```
|
462 |
|
463 | #### Messages
|
464 |
|
465 | ```js
|
466 | assert.alwaysCalledWith.message = "Expected ${0} to always be called with arguments ${1}${2}";
|
467 | refute.alwaysCalledWith.message = "Expected ${0} not to always be called with arguments${1}${2}";
|
468 | ```
|
469 |
|
470 | <dl>
|
471 | <dt>`${0}`:</dt>
|
472 | <dd>The spy</dd>
|
473 | <dt>`${1}`:</dt>
|
474 | <dd>The expected arguments</dd>
|
475 | <dt>`${2}`:</dt>
|
476 | <dd>String representation of all calls</dd>
|
477 | </dl>
|
478 |
|
479 | ### `alwaysCalledWithNew()`
|
480 |
|
481 | ```js
|
482 | assert.alwaysCalledWithNew(spy)
|
483 | ```
|
484 |
|
485 | Passes when the `spy` has was always called with `new`
|
486 |
|
487 | ```js
|
488 | var spy = this.spy();
|
489 |
|
490 | assert.alwaysCalledWithNew(spy); // Fails
|
491 |
|
492 | new spy();
|
493 | assert.alwaysCalledWithNew(spy); // Passes
|
494 |
|
495 | spy();
|
496 | assert.alwaysCalledWithNew(spy); // Fails
|
497 | ```
|
498 |
|
499 | #### Messages
|
500 |
|
501 | ```js
|
502 | assert.calledWithNew.message = "Expected ${spyObj} to always be called with 'new'";
|
503 | refute.calledWithNew.message = "Expected ${spyObj} to not always be called with 'new'";
|
504 | ```
|
505 |
|
506 | <dl>
|
507 | <dt>`${spyObj}`:</dt>
|
508 | <dd>The spy</dd>
|
509 | </dl>
|
510 |
|
511 |
|
512 | ### `calledOnceWith()`
|
513 |
|
514 | ```js
|
515 | assert.calledOnceWith(spy, arg1, arg2, ...)
|
516 | ```
|
517 |
|
518 | Passes if the `spy` was called exactly once and with the specified arguments. Other arguments may have been passed after the specified ones.
|
519 |
|
520 | ```js
|
521 | var spy = this.spy();
|
522 | var arr = [1, 2, 3];
|
523 | spy(12);
|
524 |
|
525 | assert.calledOnceWith(spy, 12); // Passes
|
526 | assert.calledOnceWith(spy, 42); // Fails
|
527 |
|
528 | spy(42, 13);
|
529 | assert.calledOnceWith(spy, 42, 13); // Fails
|
530 | ```
|
531 |
|
532 | #### Messages
|
533 |
|
534 | ```js
|
535 | assert.calledOnceWith.message = "Expected ${0} to be called once with arguments ${1}${2}";
|
536 | refute.calledOnceWith.message = "Expected ${0} not to be called once with arguments ${1}${2}";
|
537 | ```
|
538 |
|
539 | <dl>
|
540 | <dt>`${0}`:</dt>
|
541 | <dd>The spy</dd>
|
542 | <dt>`${1}`:</dt>
|
543 | <dd>The expected arguments</dd>
|
544 | <dt>`${2}`:</dt>
|
545 | <dd>String representation of all calls</dd>
|
546 | </dl>
|
547 |
|
548 | ### `calledWithExactly()`
|
549 |
|
550 | ```js
|
551 | assert.calledWithExactly(spy, arg1, arg2, ...)
|
552 | ```
|
553 |
|
554 | Passes if the `spy` was called at least once with exactly the arguments specified.
|
555 |
|
556 | ```js
|
557 | var spy = this.spy();
|
558 | var arr = [1, 2, 3];
|
559 | spy("Hey", arr, 12);
|
560 | spy("Hey", arr, 13);
|
561 |
|
562 | assert.calledWithExactly(spy, "Hey", arr, 12); // Passes
|
563 | assert.calledWithExactly(spy, "Hey", arr, 13); // Passes
|
564 | assert.calledWithExactly(spy, "Hey", arr); // Fails
|
565 | assert.calledWithExactly(spy, "Hey"); // Fails
|
566 | ```
|
567 |
|
568 | #### Messages
|
569 |
|
570 | ```js
|
571 | assert.calledWithExactly.message = "Expected ${0} to be called with exact arguments ${1}${2}";
|
572 | refute.calledWithExactly.message = "Expected ${0} not to be called with exact arguments${1}${2}";
|
573 | ```
|
574 |
|
575 | <dl>
|
576 | <dt>`${0}`:</dt>
|
577 | <dd>The spy</dd>
|
578 | <dt>`${1}`:</dt>
|
579 | <dd>The expected arguments</dd>
|
580 | <dt>`${2}`:</dt>
|
581 | <dd>String representation of all calls</dd>
|
582 | </dl>
|
583 |
|
584 | ### `alwaysCalledWithExactly()`
|
585 |
|
586 | ```js
|
587 | assert.alwaysCalledWithExactly(spy, arg1, arg2, ...)
|
588 | ```
|
589 |
|
590 | Passes if the `spy` was always called with exactly the arguments specified.
|
591 |
|
592 | ```js
|
593 | var spy = this.spy();
|
594 | var arr = [1, 2, 3];
|
595 | spy("Hey", arr, 12);
|
596 |
|
597 | assert.alwaysCalledWithExactly(spy, "Hey", arr, 12); // Passes
|
598 | assert.alwaysCalledWithExactly(spy, "Hey", arr); // Fails
|
599 | assert.alwaysCalledWithExactly(spy, "Hey"); // Fails
|
600 |
|
601 | spy("Hey", arr, 13);
|
602 | assert.alwaysCalledWithExactly(spy, "Hey", arr, 12); // Fails
|
603 | ```
|
604 |
|
605 | #### Messages
|
606 |
|
607 | ```js
|
608 | assert.alwaysCalledWithExactly.message = "Expected ${0} to always be called with exact arguments ${1}${2}";
|
609 | refute.alwaysCalledWithExactly.message = "Expected ${0} not to always be called with exact arguments${1}${2}";
|
610 | ```
|
611 |
|
612 | <dl>
|
613 | <dt>`${0}`:</dt>
|
614 | <dd>The spy</dd>
|
615 | <dt>`${1}`:</dt>
|
616 | <dd>The expected arguments</dd>
|
617 | <dt>`${2}`:</dt>
|
618 | <dd>String representation of all calls</dd>
|
619 | </dl>
|
620 |
|
621 |
|
622 | ### `threw()`
|
623 |
|
624 | ```js
|
625 | assert.threw(spy[, exception])
|
626 | ```
|
627 |
|
628 | Passes if the `spy` threw at least once the specified `exception`. The `exception` can be a string denoting its type, or an actual object. If `exception` is not specified, the assertion passes if the `spy` ever threw any exception.
|
629 |
|
630 | ```js
|
631 | var exception1 = new TypeError();
|
632 | var exception2 = new TypeError();
|
633 | var exception3 = new TypeError();
|
634 | var spy = this.spy(function(exception) {
|
635 | throw exception;
|
636 | });
|
637 |
|
638 | function callAndCatchException(spy, exception) {
|
639 | try {
|
640 | spy(exception);
|
641 | } catch(e) {
|
642 | }
|
643 | }
|
644 |
|
645 | callAndCatchException(spy, exception1);
|
646 | callAndCatchException(spy, exception2);
|
647 |
|
648 | assert.threw(spy); // Passes
|
649 | assert.threw(spy, “TypeError”); // Passes
|
650 | assert.threw(spy, exception1); // Passes
|
651 | assert.threw(spy, exception2); // Passes
|
652 | assert.threw(spy, exception3); // Fails
|
653 |
|
654 | callAndCatchException(spy, exception3); assert.threw(spy, exception3); // Passes
|
655 | ```
|
656 |
|
657 | #### Messages
|
658 |
|
659 | ```js
|
660 | assert.threw.message = "Expected ${0} to throw an exception${1}";
|
661 | refute.threw.message = "Expected ${0} not to throw an exception${1}";
|
662 | ```
|
663 |
|
664 | <dl>
|
665 | <dt>`${0}`:</dt>
|
666 | <dd>The spy</dd>
|
667 | <dt>`${1}`:</dt>
|
668 | <dd>The expected exception</dd>
|
669 | </dl>
|
670 |
|
671 | ### `alwaysThrew()`
|
672 |
|
673 | ```js
|
674 | assert.alwaysThrew(spy[, exception])
|
675 | ```
|
676 |
|
677 | Passes if the `spy` always threw the specified `exception`. The `exception` can be a string denoting its type, or an actual object. If `exception` is not specified, the assertion passes if the `spy` ever threw any exception.
|
678 |
|
679 | ```js
|
680 | var exception1 = new TypeError();
|
681 | var exception2 = new TypeError();
|
682 | var spy = this.spy(function(exception) {
|
683 | throw exception;
|
684 | });
|
685 |
|
686 | function callAndCatchException(spy, exception) {
|
687 | try {
|
688 | spy(exception);
|
689 | } catch(e) {
|
690 |
|
691 | }
|
692 | }
|
693 |
|
694 | callAndCatchException(spy, exception1);
|
695 | assert.alwaysThrew(spy); // Passes
|
696 | assert.alwaysThrew(spy, “TypeError”); // Passes
|
697 | assert.alwaysThrew(spy, exception1); // Passes
|
698 |
|
699 | callAndCatchException(spy, exception2);
|
700 | assert.alwaysThrew(spy); // Passes
|
701 | assert.alwaysThrew(spy, “TypeError”); // Passes
|
702 | assert.alwaysThrew(spy, exception1); // Fails
|
703 | ```
|
704 |
|
705 | #### Messages
|
706 |
|
707 | ```js
|
708 | assert.alwaysThrew.message = "Expected ${0} to always throw an exception${1}";
|
709 | refute.alwaysThrew.message = "Expected ${0} not to always throw an exception${1}";
|
710 | ```
|
711 |
|
712 | <dl>
|
713 | <dt>`${0}`:</dt>
|
714 | <dd>The spy</dd>
|
715 | <dt>`${1}`:</dt>
|
716 | <dd>The expected exception</dd>
|
717 | </dl>
|