HTML and CSS Reference
In-Depth Information
Again, we use Object.create to create a new fake object, assign it to a prop-
erty of the test case, and then stub ajax.create to return it. The implementation
should be straightforward, as seen in Listing 13.10.
Listing 13.10 Making a request
function start() {
if (!this.url) {
throw new TypeError("Must provide URL property");
Note that the test did not specify specifically to use ajax.request . We could
have made the request any way we wanted, so long as we used the transport provided
by ajax.create . This means, for instance, that we could carry out the aforemen-
tioned refactoring on the request interface without touching the poller tests.
Running the tests confirms that they all pass. However, the test is not quite as
concise as it could be. Knowing that the open method was called on the transport
doesn't necessarily mean that the request was sent. We'd better add an assertion
that checks that send was called as well, as Listing 13.11 shows.
Listing 13.11 Expecting request to be sent
"test start should make XHR request with URL": function () {
var poller = Object.create(ajax.poller);
poller.url = "/url";
var expectedArgs = ["GET", poller.url, true];
var actualArgs = [];
assertEquals(expectedArgs, actualArgs);
} The complete Callback
How will we issue the requests periodically? A simple solution is to make the request
through setInterval . However, doing so may cause severe problems. Issuing
new requests without knowing whether or not previous requests completed could
Search WWH ::

Custom Search