Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

mocks.$httpBackend uses mocks.$timeout to control asyncronous requests, which causes $timeout.verifyNoPendingTasks to fail unless an extra $timeout.flush(0) is called #7492

Closed
deignacio opened this issue May 16, 2014 · 2 comments

Comments

@deignacio
Copy link

Hey-

I'm trying to add in retry logic to an angular module that makes $http requests. My approach uses $timeout to allow for a delay between attempts. When writing unit tests for this module, I find that an extra $timeout.flush(0) is required to make $timeout.verifyNoPendingTasks() succeed.

Digging into ngMock, it looks like this is due to $httpbackend using $timeout to wrap responses and make then asyncronous.
https://github.com/angular/angular.js/blob/master/src/ngMock/angular-mocks.js#L1117

I have a gist that shows the failure and success once you include $timeout.flush(0)

https://gist.github.com/deignacio/c50787980fbee4c3d1db

Is there any way to decouple the two services to not require this extra flush?

@tbosch tbosch self-assigned this May 21, 2014
@tbosch tbosch added this to the Backlog milestone May 21, 2014
@tbosch tbosch removed their assignment May 21, 2014
@btford btford removed the gh: issue label Aug 20, 2014
@mbarakaja
Copy link

I am facing this same issue.

In my case, I'm using $timeout to call a function that makes use of the $http service. Before I flush everything and call $timeout.verifyNoPendingTasks(); and error like next appears:

Error: Deferred tasks to flush (1): {id: 2, time: 109999}

I need an extra call to $timeout.flush to empty the Deferred task remaining.

@gkalpak
Copy link
Member

gkalpak commented Mar 29, 2016

It's been a long time since the original post, so not sure what where the roots of the problem back then (but I know what causes it now 😃). BTW, if anyone is curious, I put the code into a runnable demo here.

I'm closing this issue in favor of #14336, where I have described the actual problem (and some background).

@mbarakaja, I am not sure your problem is the same (I am kind of buffled by time: 109999).
If you are confident it is the same issue, then let's track this in #14336. If not, please sublit a new issue providing sufficient details to reproduce the problem (a live demo works best 😛).

@gkalpak gkalpak closed this as completed Mar 29, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants