I'm attempting to use mockjax to mock my jQuery Ajax calls; however, I am not able to find any good, working examples of how to effectively test the responses.
The following code is a simple wrapper for the jQuery.Ajax object. It works but I want to write some tests around such:
Ajax Wrapper
if (!namespace){
var namespace = {};
}
namespace.Ajax = (function($){
function defaultError(xhr, err, msg){
if (typeof console !== 'undefined'){
var log = console.error;
if (!log) {
log = console.log;
}
log("==========================================");
log("An error occurred with the request.");
log("- Status Code: " + xhr.status);
log("- Status Text: " + xhr.statusText);
log("- Response Text: " + xhr.responseText);
log("- Message: " + msg);
log("==========================================");
}
}
function getData(url, data, successCallback, errorCallback){
if (!errorCallback){
errorCallback = defaultError;
}
$.ajax({
url: url
, data: data
, success: successCallback
, error: errorCallback
});
}
function postData(url, data, successCallback, errorCallback){
if (!errorCallback){
errorCallback = defaultLog;
}
$.ajax({
type: "POST"
, url: url
, data: data
, success: successCallback
, error: errorCallback
});
}
return {
getData : getData
, postData: postData
}
}(jQuery));
I have the following JsTestDriver test for the getData.
Test Class
TestCase("Ajax Object Test Fixture", {
"test Calling getData Should Return content" : function(){
var results;
var obj = namespace.Ajax;
$.mockjax({
url: "/test"
, responseTime: 1
, responseText: "success"
});
obj.getData("/test"
, null
, function(data){results = data; });
setTimeout(function(){assertEquals("'success' Should be Returned.", "success1", results);}, 500);
}
});
The assertEquals function should return false in this example since I'm expecting "success1" but throughout the code I try to set the value to "success". I want this test to fail so I know it's working. As it is though, the test succeeds. I have event tried to set the successCallback function to just be results = "success"
and it still doesn't "fail" the test.
How do I set up this test to ensure the mock response is returned so I am not getting the false positive that I have currently?