I'm trying to test what the following method returns in it's promise (functionToTest
and asyncGet
are both methods defined in an angularJS service):
var functionToTest = function (param) {
var deferred = $q.defer();
asyncGet(param).then(function (result) {
//business login involving result
if (something)
return deferred.resolve(true);
else
return deferred.resolve(false);
});
return deferred.promise;
}
The unit test looks like this:
it ('should return true', function (done) {
var asyncGetResult = {};
spyOn(asyncGet).and.returnValue($q.resolve(asyncGetResult));
var param = {};
functionToTest(param).then(function (result) {
expect(result).toBe(true);
done();
});
$scope.$apply();
});
When running the test I am getting a timeout error:
Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
I tried putting a console.log()
right after the expect()
but it does not print anything, so it seems like the callback for the functionToTest(param).then()
is never executed.
Any help would be appreciated.