This repository has been archived by the owner on Mar 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.html
563 lines (510 loc) · 19.6 KB
/
index.html
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Sinon.JS - Versatile standalone test spies, stubs and mocks for JavaScript</title>
<link rel="stylesheet" type="text/css" href="/design/css/sinon.min.css?8">
</head>
<body class="front">
<div class="payoff">
<h1>Sinon.JS</h1>
<p>
Standalone test spies, stubs and mocks for JavaScript.<br>
No dependencies, works with any unit testing framework.
</p>
</div>
<ul class="nav main-nav">
<li><a href="/docs">Documentation</a></li>
<li><a href="#help">Help</a></li>
<li><a href="https://github.com/cjohansen/Sinon.JS/issues">Issues</a></li>
<li><a href="https://github.com/cjohansen/Sinon.JS">Source code</a></li>
</ul>
<div class="download section">
<div class="content">
<div class="button"><a href="releases/sinon-1.8.2.js">Download Sinon.JS 1.8.2</a></div>
<p>
2014-02-11 - <a href="Changelog.txt">Changelog</a> - <a href="/download">More builds/versions</a>
</p>
</div>
<div class="browsers">
<ul>
<li class="ie"><span>Internet Explorer</span> 6.0+</li>
<li class="ff"><span>Firefox</span> 2.0+</li>
<li class="chrome"><span>Chrome</span> 7.0+</li>
<li class="opera"><span>Opera</span> 10.10+</li>
<li class="safari"><span>Safari</span> 3.2+</li>
<li class="mobile-safari"><span>Mobile Safari</span> 3.2+</li>
<li class="android-browser"><span>Android browser</span> 2.1+</li>
<li class="node-js"><span>Node.js</span> 0.2.x+</li>
</ul>
</div>
</div>
<div class="section" id="five-min">
<div class="content">
<h2>Getting started</h2>
<p>
The following function takes a function as its argument and returns a
new function. You can call the resulting function as many times as you
want, but the original function will only be called once:
</p>
<pre class="sh_javascript"><code>function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}</code></pre>
<h3>Spies</h3>
<p>
Testing this function can be quite elegantly achieved with a
<a href="/docs#spies">test spy</a>:
</p>
<pre class="sh_javascript"><code>it("calls the original function", function () {
var callback = sinon.spy();
var proxy = once(callback);
proxy();
assert(callback.called);
});</code></pre>
<p>
The fact that the function was only called once is important:
</p>
<pre class="sh_javascript"><code>it("calls the original function only once", function () {
var callback = sinon.spy();
var proxy = once(callback);
proxy();
proxy();
assert(callback.calledOnce);
// ...or:
// assert.equals(callback.callCount, 1);
});</code></pre>
<p>
We also care about the <code>this</code> object and arguments:
</p>
<pre class="sh_javascript"><code>it("calls original function with right this and args", function () {
var callback = sinon.spy();
var proxy = once(callback);
var obj = {};
proxy.call(obj, 1, 2, 3);
assert(callback.calledOn(obj));
assert(callback.calledWith(1, 2, 3));
});</code></pre>
<p><a href="/docs#spies">Learn more about spies</a>.</p>
<h3>Stubs</h3>
<p>
The function returned by <code>once</code> should return whatever the
original function returns. To test this, we create
a <a href="/docs#stubs">stub</a>:
</p>
<pre class="sh_javascript"><code>it("returns the return value from the original function", function () {
var callback = sinon.stub().returns(42);
var proxy = once(callback);
assert.equals(proxy(), 42);
});</code></pre>
<p>
Conveniently, stubs can also be used as spies, e.g. we can query them
for their <code>callCount</code>, received <code>args</code> and more.
</p>
<p><a href="/docs#stubs">Learn more about stubs</a>.</p>
<h3>Testing Ajax</h3>
<p>
The following function triggers network activity:
</p>
<pre class="sh_javascript"><code>function getTodos(listId, callback) {
jQuery.ajax({
url: "/todo/" + listId + "/items",
success: function (data) {
// Node-style CPS: callback(err, data)
callback(null, data);
}
});
}</code></pre>
<p>
To test this function without triggering network activity we could
stub <code>jQuery.ajax</code>:
</p>
<pre class="sh_javascript"><code>after(function () {
// When the test either fails or passes, restore the original
// jQuery ajax function (Sinon.JS also provides tools to help
// test frameworks automate clean-up like this)
jQuery.ajax.restore();
});
it("makes a GET request for todo items", function () {
sinon.stub(jQuery, "ajax");
getTodos(42, sinon.spy());
assert(jQuery.ajax.calledWithMatch({ url: "/todo/42/items" }));
});</code></pre>
<h3>Fake XMLHttpRequest</h3>
<p>
While the preceding test shows off some nifty Sinon.JS tricks, it is
too tightly coupled to the implementation. When testing Ajax, it is
better to use Sinon.JS' <a href="/docs/#server">fake
XMLHttpRequest</a>:
</p>
<pre class="sh_javascript"><code>var xhr, requests;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = function (req) { requests.push(req); };
});
after(function () {
// Like before we must clean up when tampering with globals.
xhr.restore();
});
it("makes a GET request for todo items", function () {
getTodos(42, sinon.spy());
assert.equals(requests.length, 1);
assert.match(requests[0].url, "/todo/42/items");
});</code></pre>
<p><a href="/docs#FakeXMLHttpRequest">Learn more about fake XMLHttpRequest</a>.</p>
<h3>Fake server</h3>
<p>
The preceding example shows how flexible this API is. If it looks too
laborous, you may like the fake server:
</p>
<pre class="sh_javascript"><code>var server;
before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });
it("calls callback with deserialized data", function () {
var callback = sinon.spy();
getTodos(42, callback);
// This is part of the FakeXMLHttpRequest API
server.requests[0].respond(
200,
{ "Content-Type": "application/json" },
JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
);
assert(callback.calledOnce);
});</code></pre>
<p>
Test framework integration can typically reduce boilerplate further.
<a href="/docs#fakeServer">Learn more about the fake server</a>.
</p>
<h2>Faking time</h2>
<blockquote>
<p>
"I don't always bend time and space in unit tests, but when I do, I
use Buster.JS + Sinon.JS"
</p>
<cite><a href="https://twitter.com/briancavalier/status/225617077346635776">Brian Cavalier, Cujo.JS</a></cite>
</blockquote>
<p>
Testing time-sensitive logic without the wait is a breeze with
Sinon.JS. The following function throttles another function - only
when it has not been called for 100 milliseconds will it call the
original function with the last set of arguments it received.
</p>
<pre class="sh_javascript"><code>function throttle(callback) {
var timer;
return function () {
clearTimeout(timer);
var args = [].slice.call(arguments);
timer = setTimeout(function () {
callback.apply(this, args);
}, 100);
};
}</code></pre>
<p>
Thanks to Sinon.JS' time-bending abilities, testing it is easy:
</p>
<pre class="sh_javascript"><code>var clock;
before(function () { clock = sinon.useFakeTimers(); });
after(function () { clock.restore(); });
it("calls callback after 100ms", function () {
var callback = sinon.spy();
var throttled = throttle(callback);
throttled();
clock.tick(99);
assert(callback.notCalled);
clock.tick(1);
assert(callback.calledOnce);
// Also:
// assert.equals(new Date().getTime(), 100);
}</code></pre>
<p>
As before, Sinon.JS provides utilities that help test frameworks
reduce the boiler-plate.
<a href="/docs#clock">Learn more about fake time</a>.
</p>
<h2>And all the rest...</h2>
<p>
You've seen the most common tasks people tackle with Sinon.JS, yet
we've only scratched the surface. View more quick examples below, or
dive into the <a href="/docs">API docs</a>, which also provides useful
pointers on how and when to use the various functionality.
</p>
</div>
</div>
<div class="examples section">
<div class="content" id="examples">
<h2>Features at a glance</h2>
<ul class="nav">
<li class="active"><a href="#spy_example">Spies</a></li>
<li><a href="#stub_example">Stubs</a></li>
<li><a href="#mock_example">Mocks</a></li>
<li><a href="#timers_example">Fake timers</a></li>
<li><a href="#xhr_example">Fake XHR</a></li>
<li><a href="#server_example">Fake server</a></li>
<li><a href="#sandbox_example">Sandboxing</a></li>
<li><a href="#assertion_example">Assertions</a></li>
<li><a href="#matcher_example">Matchers</a></li>
</ul>
<div id="spy_example">
<pre class="sh_javascript"><code>// Function under test
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
it("calls the original function", function () {
var spy = sinon.spy();
var proxy = once(spy);
proxy();
assert(spy.called);
});</code></pre>
<p><a href="docs/#spies">Spy documentation</a>.</p>
</div>
<div id="stub_example">
<pre class="sh_javascript"><code>// Function under test
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
it("returns the return value from the original function", function () {
var stub = sinon.stub().returns(42);
var proxy = once(stub);
assert.equals(proxy(), 42);
});</code></pre>
<p><a href="docs/#stubs">Stub documentation</a>.</p>
</div>
<div id="mock_example">
<pre class="sh_javascript"><code>// Function under test
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
it("returns the return value from the original function", function () {
var myAPI = { method: function () {} };
var mock = sinon.mock(myAPI);
mock.expects("method").once().returns(42);
var proxy = once(myAPI.method);
assert.equals(proxy(), 42);
mock.verify();
});</code></pre>
<p><a href="docs/#mocks">Mock documentation</a>.</p>
</div>
<div id="timers_example">
<pre class="sh_javascript"><code>// Function under test
function throttle(callback) {
var timer;
return function () {
clearTimeout(timer);
var args = [].slice.call(arguments);
timer = setTimeout(function () {
callback.apply(this, args);
}, 100);
};
}
var clock;
before(function () { clock = sinon.useFakeTimers(); });
after(function () { clock.restore(); });
it("calls callback after 100ms": function () {
var callback = sinon.spy();
var throttled = throttle(callback);
throttled();
clock.tick(99);
assert(callback.notCalled);
clock.tick(1);
assert(callback.calledOnce);
// Also:
// assert.equals(new Date().getTime(), 100);
}</code></pre>
<p><a href="docs/#clock">Timers documentation</a></p>
</div>
<div id="xhr_example">
<pre class="sh_javascript"><code>// Function under test
function getTodos(listId, callback) {
jQuery.ajax({
url: "/todo/" + listId + "/items",
success: function (data) {
// Node-style CPS: callback(err, data)
callback(null, data);
}
});
}
var xhr, requests;
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = function (req) { requests.push(req); };
});
after(function () {
// Like before we must clean up when tampering with globals.
xhr.restore();
});
it("makes a GET request for todo items", function () {
getTodos(42, sinon.spy());
assert.equals(requests.length, 1);
assert.match(requests[0].url, "/todo/42/items");
});</code></pre>
<p><a href="docs/#server">Fake XHR/server documentation</a></p>
</div>
<div id="server_example">
<pre class="sh_javascript"><code>// Function under test
function getTodos(listId, callback) {
jQuery.ajax({
url: "/todo/" + listId + "/items",
success: function (data) {
// Node-style CPS: callback(err, data)
callback(null, data);
}
});
}
var server;
before(function () { server = sinon.fakeServer.create(); });
after(function () { server.restore(); });
it("calls callback with deserialized data", function () {
var callback = sinon.spy();
getTodos(42, callback);
// This is part of the FakeXMLHttpRequest API
server.requests[0].respond(
200,
{ "Content-Type": "application/json" },
JSON.stringify([{ id: 1, text: "Provide examples", done: true }])
);
assert(callback.calledOnce);
});</code></pre>
<p><a href="docs/#server">Fake XHR/server documentation</a></p>
</div>
<div id="sandbox_example">
<pre class="sh_javascript"><code>// Function under test
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
it("returns the return value from the original function", sinon.test(function () {
var myAPI = { method: function () {} };
var mock = sinon.mock(myAPI);
mock.expects("method").once().returns(42);
var proxy = once(myAPI.method);
assert.equals(proxy(), 42);
}));</code></pre>
<p>
<code>sinon.test()</code> provides automatic restoring of
stubbed/mocked globals, automatic mock verification and
more. <a href="docs/#sandbox">Sandbox documentation</a>
</p>
</div>
<div id="assertion_example">
<pre class="sh_javascript"><code>it("makes a GET request for todo items", function () {
sinon.stub(jQuery, "ajax");
getTodos(42, sinon.spy());
sinon.assert.calledOnce(jQuery.ajax);
sinon.assert.calledWithMatch(jQuery.ajax, { url: "/todo/42/items" });
});</code></pre>
<p><a href="docs/#assertions">Assertions documentation</a></p>
</div>
<div id="matcher_example">
<pre class="sh_javascript"><code>it("tests partial argument matches, regexen and more", function () {
var spy = sinon.spy();
spy("Go", function () { /* ... */ });
assert(spy.calledWith("message", sinon.match.func));
assert(spy.calledWithMatch(/[a-z]+/));
});</code></pre>
<p><a href="docs/#matchers">Matcher documentation</a>.</p>
</div>
</div>
</div>
<div class="section docs-intro">
<div class="content">
<h2>Documentation</h2>
<ul>
<li><a href="docs/">Sinon.JS documentation</a></li>
<li><a href="http://cjohansen.no/talks/2011/xp-meetup/">105 slides on Sinon.JS</a></li>
<li><a href="http://cjohansen.no/en/sinon_js/sinon_js_1_3_0_out_now">What's new in 1.3.0?</a></li>
<li><a href="http://cjohansen.no/en/sinon_js/sinon_js_1_1_0_out_now">What's new in 1.1.0?</a></li>
</ul>
</div>
</div>
<div class="adapters section">
<div class="content">
<h2>Test framework adapters</h2>
<ul>
<li><a href="http://demo.qooxdoo.org/current/apiviewer/#qx.dev.unit.MMock">qooxdoo (<code>qx.dev.unit.MMock</code>)</a></li>
<li><a href="https://github.com/froots/jasmine-sinon">Jasmine matchers</a></li>
<li><a href="https://github.com/domenic/sinon-chai">Chai assertions</a></li>
<li><a href="qunit/">Sinon.JS and QUnit</a></li>
</ul>
</div>
</div>
<div class="section" id="help">
<div class="content">
<h2>Get help</h2>
<ul>
<li>
<a href="http://groups.google.com/group/sinonjs">Sinon.JS mailing list</a>
</li>
<li>IRC: #sinon.js on freenode</li>
</ul>
</div>
</div>
<div class="section" id="help">
<div class="content">
<h2>Sinon.JS elsewhere</h2>
<ul>
<li><a href="http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html">Testing Backbone applications with Jasmine and Sinon</a></li>
<li><a href="http://cjohansen.no/en/javascript/using_sinon_js_with_qunit">Using Sinon.JS with QUnit</a></li>
<li><a href="http://cjohansen.no/en/javascript/sinon_js_0_6_0_fake_xmlhttprequest_and_improved_test_framework_integration">Faking XMLHttpRequests with Sinon.JS 0.6.0</a></li>
<li><a href="http://cjohansen.no/en/javascript/sinon_js_0_5_0_released">Sinon.JS 0.5.0 announcement</a></li>
<li><a href="http://cjohansen.no/en/javascript/faking_timers_and_dates_with_sinon">Faking timers and dates with Sinon.JS</a></li>
<li><a href="http://cjohansen.no/en/javascript/higer_level_stubbing_and_mocking_tools_in_sinon">Sinon.JS test framework integration tools</a></li>
<li><a href="http://cjohansen.no/en/javascript/test_spies_stubs_and_mocks_part_1_5">More
spies, stubs and mocks with Sinon.JS</a></li>
<li><a href="http://cjohansen.no/en/javascript/javascript_test_spies_stubs_and_mocks">Original Sinon.JS announcement</a></li>
</ul>
<p>
My book, <a href="http://tddjs.com">Test-Driven JavaScript Development</a>
covers some of the design philosophy and initial sketches for Sinon.JS.
</p>
</div>
</div>
<div class="aside">
<p>Sinon uses <a href="http://semver.org/">Semantic versioning</a>.</p>
<p>
Copyright 2010 - 2014,
<a href="http://cjohansen.no/">Christian Johansen</a>. Released under the
<a href="http://www.opensource.org/licenses/bsd-license.php">BSD license</a>.
</p>
<p class="tweet"><a href="http://twitter.com/share" data-lang="en" data-via="cjno" class="twitter-share-button">Tweet this</a></p>
</div>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<script type="text/javascript" src="/design/js/sinon-web.min.js?4"></script>
<script type="text/javascript" src="/releases/sinon.js"></script>
<script type="text/javascript" src="/releases/sinon-ie.js"></script>
</body>
</html>