alex3683
alex3683

Reputation: 1565

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.

Upvotes: 3

Views: 2266

Answers (1)

Alex Vayda
Alex Vayda

Reputation: 6494

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

Upvotes: 7

Related Questions