HTML and CSS Reference
In-Depth Information
Whereas we had no choice of where to stub while developing
ajax.request
(it only depended on the
XMLHttpRequest
object via the
ajax.create
method),
we
now
have
the
opportunity
to
choose
if
we
want
to
stub
ajax.request
or
ajax.create
. We will try a slightly different approach in this chapter by
stubbing “lower.” This makes our tests mini integration tests, as discussed in
Chapter 1,
Automated Testing,
with the pros and cons that follow. However, as we
have just developed a reasonable test suite for
ajax.request
, we should be able
to trust it for the cases we covered in Chapter 12,
Abstracting Browser Differences:
Ajax.
While developing the poller we will strive to fake as little as possible, but we
need to cut off the actual server requests. To do this we will simply keep using the
fakeXMLHttpRequest
object from Chapter 12,
Abstracting Browser Differences:
Ajax.
13.1.2.4 The First Request
To specify that the
start
method should start polling, we need to assert somehow
that a URL made it across to the
XMLHttpRequest
object. To do this we assert
that its
open
method was called with the expected URL, as seen in Listing 13.9.
Listing 13.9
Expecting the poller to issue a request
setUp: function () {
this.ajaxCreate = ajax.create;
this.xhr = Object.create(fakeXMLHttpRequest);
ajax.create = stubFn(this.xhr);
},
tearDown: function () {
ajax.create = this.ajaxCreate;
},
/* ... */
"test start should make XHR request with URL": function () {
var poller = Object.create(ajax.poller);
poller.url = "/url";
poller.start();
assert(this.xhr.open.called);
assertEquals(poller.url, this.xhr.open.args[1]);
}