Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using $timeout-Mock in AngularJS with delay

I've used $timeout with a delay of e.g. 600ms in some service code. In the according test I want to assert that the code really does what it is expected to do after the given 600 ms. Is there any way to progress in time with a certain amount of time (similar to jasmine.Clock.tick(600))? I only know about $timeout.flush(), but that fires everything currently in the $timeout-queue.

like image 829
alex3683 Avatar asked Sep 15 '26 05:09

alex3683


1 Answers

In AngularJS version 1.2 you can do $timeout.flush(600), which is analogous to Jasmine's jasmine.Clock.tick(600).

In addition to that Angular 1.2 provides a very handy $timeout.flushNext(msec) method. It differs from the flush(msec) in that that instead of simply putting a clock given time ahead for you to then check if an expected thing happened or not, the flushNext() method does both tasks at once. It puts a clock ahead up until the next deferred event is fired and then verifies that the elapsed time equals the given one.

Link to the documentation

like image 186
Alex Vayda Avatar answered Sep 16 '26 20:09

Alex Vayda