HTML and CSS Reference
In-Depth Information Introducing
With ajax.request in place, implementing POST requests should be a breeze.
Feeling brave, we skip the simple test to prove the method's existence this time
around. Instead, the test in Listing 12.55 shows how we expect the method to
Listing 12.55 Expecting to delegate to ajax.request
TestCase("PostRequestTest", {
setUp: function () {
this.ajaxRequest = ajax.request;
tearDown: function () {
ajax.request = this.ajaxRequest;
"test should call request with POST method": function () {
ajax.request = stubFn();"/url");
assertEquals("POST", ajax.request.args[1].method);
Implementation is trivial, as seen in Listing 12.56.
Listing 12.56 Delegating to ajax.request with POST as method
function post(url, options) {
options = tddjs.extend({}, options);
options.method = "POST";
ajax.request(url, options);
} = post;
Running the tests confirms that this implementation solves the newly added
requirement. As always, we look for duplication before moving on. Obviously, the
get and post methods are very similar. We could extract a helper method, but
saving only two lines in two methods at the expense of another function call and
another level of indirection doesn't seem worthwhile at this point. You may feel
Search WWH ::

Custom Search