Below is the code snippet from FormService.js:
service.retriveFields = function (Id,UniqueIds) {
var deferred = $q.defer();
function success(successResponse) {
// valid response received
if (successResponse.data) {
deferred.resolve(successResponse.data);
} else {
// TODO: show error message
console.log("error");
deferred.reject(response);
}
}
function error(errorResponse) {
console.log(errorResponse);
deferred.reject(errorResponse);
// TODO: show error message
}
// service call
SomeFactory.retriveFields(Id, UniqueIds)
.then(success, error);
return deferred.promise;
};
The following test case has been added:
describe('retriveFields ()', function () {
it('tests function call', function (done) {
var response = {data: {}};
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function () {
resolve(response);
});
var Id = "5b7d86e60bf5e5bc21b4309f";
var UniqueIds = ["5b7d71b822fc4e102c0875d3", "5b7d71ad2d4914162cb12ec6"];
sinon.stub(SomeFactory, 'retriveFields')
.returns($q.when(response));
var promise = FormService.retriveFields (Id,UniqueIds);
promise.then(function (result) {
expect(result).toEqual({data :{}});
})
});
});
However, an error was encountered:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
The reason for this timeout is unclear. Any suggestions on how to correctly test the defer functionality?