Last active
April 19, 2016 09:59
-
-
Save pedroadaodev/2223a99257a73632be2d17aa1d93343f to your computer and use it in GitHub Desktop.
qUnit Test framework to help test my WebApi endpoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More information about this code here:
http://adaodeveloper.blogspot.pt/2013/01/unit-test-web-services-with-qunit.html