HTML and CSS Reference
In-Depth Information
Listing 12.49 Using the request helper in tests
"test should call success handler for status 200":
function () {
var request = forceStatusAndReadyState(this.xhr, 200, 4);
/* ... */
"test should call success handler for local requests":
function () {
tddjs.isLocal = stubFn(true);
var request = forceStatusAndReadyState(this.xhr, 0, 4);
When making big changes like this I like to introduce a few intentional bugs
in the helper to make sure it's working as I expect. For instance, we could com-
ment out the line that sets the success handler in the helper to verify that the test
then fails. Also, the second test should fail if we comment out the line that stubs
tddjs.isLocal to return true , which it does. Manipulating the ready state and
status code is also a good way to ensure tests still behave as expected. Further Status Code Tests
Using the new helper makes testing for new status codes a trivial task, so I will leave
it as an exercise. Although testing for more status codes and making sure the failure
callback is fired for status codes outside the 200 range (with the exception of 0 for
local files and 304 “Not Modified”) is a good exercise in test-driven development,
doing so will add little new to our discussion. I urge you to run through the steps as
an exercise, and when you are done you could always compare your quest to mine
by downloading the sample code off the topic's website 2 .
Search WWH ::

Custom Search