Testing on the Toilet

March 29, 2007

JavaScript: Simulating

Time in jsUnit Tests

Sometimes you need to test client-side JavaScript code that uses setTimeout() to do some work in the future. jsUnit contains the Clock.tick() method, which simulates time passing without causing the test to sleep. For example, this function will set up some callbacks to update a status message over the course of four seconds: function showProgress(status) { status.message = "Loading"; for (var time = 1000; time <= 3000; time+= 1000) { // Append a '.' to the message every second for 3 secs. setTimeout(function() { status.message += "."; }, time); } setTimeout(function() { // Special case for the 4th second. status.message = "Done"; }, 4000); }

The jsUnit test for this function would look like this: function testUpdatesStatusMessageOverFourSeconds() { Clock.reset(); // Clear any existing timeout functions on the event queue. var status = {}; showProgress(status); // Call our function. assertEquals("Loading", status.message); Clock.tick(2000);

// Call any functions on the event queue that have been // scheduled for the first two seconds. assertEquals("Loading..", status.message); Clock.tick(2000); // Same thing again, for the next two seconds. assertEquals("Done", status.message); }

This test will run very quickly - it does not require four seconds to run. Clock supports the functions setTimeout(), setInterval(), clearTimeout(), and clearInterval(). The Clock object is defined in jsUnitMockTimeout.js, which is in the same directory as jsUnitCore.js.

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2007 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

Time in jsUnit Tests Code

Mar 29, 2007 - function testUpdatesStatusMessageOverFourSeconds() {. Clock.reset(); // Clear any existing timeout functions on the event queue. var status = ...

163KB Sizes 14 Downloads 191 Views

Recommend Documents

Simulating Time in jsUnit Tests
Oct 2, 2008 - Sometimes you need to test client-side JavaScript code that uses setTimeout() to do some work in the future. jsUnit contains the Clock.tick() method, which simulates time passing without causing the test to sleep. For example, this func

Simulating Time in jsUnit Tests
Oct 2, 2008 - Sometimes you need to test client-side JavaScript code that uses ... Clock.reset(); // Clear any existing timeout functions on the event queue.

Nonparametric Tests of the Markov Hypothesis in Continuous-Time ...
Dec 14, 2010 - Princeton University and NBER, Princeton University and University of .... Under time-homogeneity of the process X, the Markov hypothesis can ...

Naming Unit Tests Responsibly Code
Jan 30, 2007 - For example, when it's hard to construct a sentence where the first word is the class under test, it suggests the test may be in the wrong place. And classes that are hard to describe in general often need to be broken down into smalle

Refactoring Tests in the Red
Apr 25, 2007 - With a good set of tests in place, refactoring code is much easier, as you can quickly gain a lot of confidence by running the tests again and ...

The time course oforthographic and phonological code ...
associative, and grammatical context effects with identified and uniden- tified primes. Language & Speech, 33, 1-18. LUKATELA, G., & TURVEY, M. T. (l990a).

Distributed Space-Time Trellis Code for Asynchronous ...
Jun 20, 2008 - time block codes (STBC) is used and the perfect synchronization assumption is ... time code that achieves full cooperative diversity without ...

Distributed Space-Time Trellis Code for Asynchronous ...
Jun 20, 2008 - gain through the exhaustive computer search. ... with minimum memory order for asynchronous cooperative communications where the ...

Space Time Block Code for CDMA2000 with Pilot ...
Abstract--The space time block code (STBC) has recently attracted much attention for its ability to achieve significant diversity gain. And most of the papers ...

Code Mutation in Verification and Automatic Code ...
sender and receiver side. The protocol we check appears both in a .... The action is a sequence of statements, exe- cuted when the condition holds. in addition, ...

Code Mutation in Verification and Automatic Code ...
Model checking is a successful technique for comparing a model of a system with .... metric communication construct of the programming language ADA.

A-Wrinkle-In-Time-Madeleine-L-Engle-s-Time-Quintet.pdf
A-Wrinkle-In-Time-Madeleine-L-Engle-s-Time-Quintet.pdf. A-Wrinkle-In-Time-Madeleine-L-Engle-s-Time-Quintet.pdf. Open. Extract. Open with. Sign In.

A-Wrinkle-In-Time-Time-Quintet.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. A-Wrinkle-In-Time-Time-Quintet.pdf. A-Wrinkle-In-Time-Time-Quintet.pdf. Open. Extract. Open with. Sign In. M

time time time -
Page 1. time. 0.985. 0.97. 0.015. 0.03. 2.3. 0.04. 0.189. 2.31. 2.89. 2.35. Gma. Gch. Gmo_N2. Gmo_I3. Gmo_I7. Gmo_C7. Bsa time. 0.97. 0.03. 0.037. 0.181. 2.32.