So I thought my latest code might help -
Unit test -
it('Timeout logs Timed out', function(){
spyOn(console, 'log');
scope.qPromiseCall();
$timeout.flush(251);
$rootScope.$apply();
expect(console.log).toHaveBeenCalledWith("Timed out");
})
Declaration of the service. I'm setting promise.promise manually in other
tests before calling $rootScope.apply(), returning the undefined promise
simulates a timeout ( doesn't it??? )
mockPromiseService = jasmine.createSpyObj('promiseService', ['getPromise']);
mockPromiseService.getPromise.andCallFake( function() {
promise = $q.defer();
return promise.promise;
})
The controller function I'm testing
$scope.qPromiseCall = function() {
promise = null;
$timeout(function() {
promise = promiseService.getPromise();
promise.then(function (data) {
if (data == "promise success!") {
console.log("success");
} else {
console.log("function failure");
}
}, function (error) {
console.log("promise failure")
}
);
}, 250).then(function (data) {
if(data == undefined ) {
console.log("Timed out")
}
},function( error ){
console.log("timed out!");
});
}
how do I differentiate between a timed out trigger to the last function in
the chain (undefined promise) and a successful call (or rejected) where
the promise is defined?
On Thursday, April 10, 2014 6:56:32 PM UTC+1, Liam Ryan wrote:
>
> Hi All,
>
> I've posted this on SO but thought I'd ask at the source too
>
>
> http://stackoverflow.com/questions/22994871/setting-a-timeout-handler-on-a-promise-in-angularjs
>
> <http://stackoverflow.com/questions/22994871/setting-a-timeout-handler-on-a-promise-in-angularjs>
>
> What I want is a handler function that will kick in if my promise hasn't
> been returned in x ms and take over from there which will not break
> pre-existing unit tests.
>
> Any ideas? I'm pretty sure this must be a standard thing but my google-fu
> has failed me
>
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.