HTML and CSS Reference
In-Depth Information
TestCase("GetRequestTest", {
/* ... */
"test should call open with method, url, async flag":
function () {
var actual;
ajax.create = stubFn({
open: function () {
actual = arguments;
}
});
var url = "/url";
ajax.get(url);
assertEquals(["GET", url, true], actual);
}
});
We expect this test to fail because the open method isn't currently being called
from our implementation, implying that actual should be undefined. This is
exactly what happens and so we can write the implementation, as in Listing 12.21.
Listing 12.21 Calling open
function get(url) {
/* ... */
transport.open("GET", url, true);
}
Now a few interesting things happen. First, we hardcoded both the HTTP
verb and the asynchronous flag. Remember, one step at a time; we can make those
configurable later. Running the tests shows that whereas the current test succeeds,
the previous test now fails. It fails because the stub in that test did not return an
object, so our production code is attempting to call undefined.open , which
obviously won't work.
The second test uses the stubFn function to create one stub, while manually
creating a stub open method in order to inspect its received arguments. To fix these
problems, we will improve stubFn and share the fake XMLHttpRequest object
between tests.
 
Search WWH ::




Custom Search