I've been working on writing Jasmine unit tests for a JavaScript file that includes an Ajax call. I'm unsure how to properly test the 'Success' function using Jasmine.
Here's a snippet of the code:
function getGroups(){
var defer = _$q.defer();
$.ajax( {
url: 'xyz',
data: 'abc',
async: false,
type: 'POST',
dataType: 'xml',
success: function( xml ) {
var groups = [];
groups = xml.getElementsByTagName( 'requiredGroup' );
defer.resolve( groups );
}
} );
return defer.promise;
}
I've attempted various solutions, but haven't been successful in covering the code within the success function:
describe("getGroups() does ajax call", function() {
beforeEach(function() {
spyOn($, 'ajax');
});
it("should call the success function", function() {
getGroups();
expect($.ajax).toHaveBeenCalled();
});
});