HTML and CSS Reference
In-Depth Information
This helper contains enough logic that it should not be simply dropped into the
project without tests. Testing the helper is left as an exercise. Testing with Stubbed Dates
Now that we have a way of faking time, we can formulate the test that expects new
requests to be made immediately if the minimum interval has passed since the last
request was issued. Listing 13.36 shows the test.
Listing 13.36 Expecting long-running request to immediately re-connect
upon completion
TestCase("PollerTest", {
setUp: function () {
/* ... */
this.ajaxRequest = ajax.request;
/* ... */
tearDown: function () {
ajax.request = this.ajaxRequest;
/* ... */
/* ... */
"test should re-request immediately after long request":
function () {
this.poller.interval = 500;
var ahead = new Date().getTime() + 600;
stubDateConstructor(new Date(ahead));
ajax.request = stubFn();
The test sets up the poller interval to 500ms, and proceeds to simulate a request
lasting for 600ms. It does this by making new Date return an object 600ms into
the future, and then uses this.xhr.complete() to complete the fake request.
Once this happens, the minimum interval has elapsed since the previous request
Search WWH ::

Custom Search