Example of using mockjax and qunit to test ajax call

asyncTest(“Test successful getTradePeriod Ajax call should fill out trade period dropdown list”, function(){
$(‘<select id=”tradePeriod”></select>’)
.appendTo(“#qunit-fixture”);

$.mockjax({
url: ‘/DATData/getTradePeriod’
,type: ‘POST’
,responseTime: 100
,dataType: ‘xml’
,responseXML:'<tradePeriods>’
+   ‘<data><fromDate>2014-10-01</fromDate><toDate>2014-10-08</toDate></data>’
+   ‘<data><fromDate>2014-10-09</fromDate><toDate>2014-10-16</toDate></data>’
+ ‘</tradePeriods>’
});

equal($(‘#tradePeriod option’).size(), 0);

loadTradePeriod();

setTimeout(function() {
equal($(‘#tradePeriod option’).size(), 2);
start();
}, 200);
});

Tip: 1. use asyncTest; 2. put expected wait time (longer than response time) in setTimeout function.

Advertisements