Skip to content

Instantly share code, notes, and snippets.

@pedroadaodev
Last active April 19, 2016 09:59
Show Gist options
  • Save pedroadaodev/2223a99257a73632be2d17aa1d93343f to your computer and use it in GitHub Desktop.
Save pedroadaodev/2223a99257a73632be2d17aa1d93343f to your computer and use it in GitHub Desktop.
qUnit Test framework to help test my WebApi endpoints
(function () {
//name of the Unit Test Module
module('Sample');
var indexOn = 0;
//object array with the webservices to call
var apiUrls = [
{
name: "GetAll", //name
numberOfTests: 1, //number of tests to be done
url: '/base/example1/GetAll', //url of my webservice
type: 'GET', //ajax call type
callbackSucess: function (result) { //callback function
ok(result.Success, 'GetAll'); //my assert
},
callbackError: function (result) { //callback function
ok(!result.Success, 'GetAll ERROR'); //my assert
}
},
{
name: "Search",
numberOfTests: 1,
url: '/base/example1/Search',
type: 'POST',
data: { //data to be pass
'Context': 'dummy',
'Term': 'termOfTheSearch'
},
callbackSucess: function (result) {
ok(result.Success, 'Search');
},
callbackError: function (result) { //callback function
ok(!result.Success, 'GetAll ERROR'); //my assert
}
}
];
//Call all webservices
var next = function () {
if (indexOn < apiUrls.length) {
var apiUrl = apiUrls[indexOn];
asyncTest('call: ' + apiUrl.name, apiUrl.numberOfTests, function () {
var dataStringify = apiUrl.data;
if (apiUrl.type != 'GET' && !$.isEmptyObject(apiUrl.data)) {
dataStringify = JSON.stringify(apiUrl.data);
}
$.ajax({
url: apiUrl.url,
dataType: 'json',
contentType: 'application/json',
data: dataStringify,
type: apiUrl.type,
success: function (result) {
apiUrl.callbackSucess(result);
start(); // qunit call to make all asserts
next(); //call next webservice
},
error: function (result) {
apiUrl.callbackError(result);
start(); // qunit call to make all asserts
next(); //call next webservice
}
});
});
indexOn++;
}
};
next(); //start the webservice calls
})();
@pedroadaodev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment