HTML and CSS Reference
In-Depth Information
this to the usual stubbing approach in which we stub the timer, do some work and
then assert that the stub was used as expected. Stubbing yields shorter tests, but
using the clock yields more communicative tests. We will use the clock to test the
poller to get a feel of the difference.
The jsUnitMockTimeout.js can be downloaded off the topic's website. 2
Copy it into the project's lib directory. Scheduling New Requests
In order to test that the poller schedules new requests we need to:
Create a poller with a URL
Start the poller
Simulate the first request completing
Stub the send method over again
Fast-forward time the desired amount of milliseconds
Assert that the send method is called a second time (this would have been
called while the clock passed time)
To complete the request we will add yet another helper to the fakeXML-
HttpRequest object, which sets the HTTP status code to 200 and calls the on-
readystatechange handler with ready state 4. Listing 13.15 shows the new
Listing 13.15 Adding a helper method to complete request
var fakeXMLHttpRequest = {
/* ... */
complete: function () {
this.status = 200;
Using this method, Listing 13.16 shows the test following the above require-
Search WWH ::

Custom Search