The common question that comes to mind is how to test asynchronous (Ajax) JavaScript code using YUI Test. What was mentioned earlier in this chapter so far is how to perform unit testing for synchronous JavaScript code. YUI Test provides two main APIs in order to perform real Ajax testing: wait()
and resume()
. Although the provided APIs of the YUI Test to perform real Ajax testing are not as rich as Jasmine (the provided YUI Test APIs do not, for example include something like spies or the Jasmine's automatic waitsFor
mechanism), they are enough to perform a real Ajax test. Let me show you how to do this.
The wait()
function
has two modes of operation. Its first mode pauses the execution of the test until its timeout period passes. For example:
this.wait(function() { Y.Assert.isAverage(3, 4, 3.5, "Average is incorrect"); }, 1000);
This code pauses the test for 1000
milliseconds, and after that its function in the first...