vendredi 20 novembre 2015

In Angular unit testing, how to clean-up $timeout promises so they don't persist?

I have a series of unit tests. One of the controllers is setting a $timeout promise which executes every 1000 ms (it invokes its own function, so it behaves like an $interval, continues executing every 1 second as long as the controller is being viewed). However, when the controller is destroyed (user navigates to another view), the $timeout.cancel is called accurately, thus the self-invoking ceases to continue.

But when running unit tests, for some reason after the test for this controller is executed, the $destroy is never triggered.

So when second unit test is run (in this case a unit test for a directive), which also uses $timeout.flush(...), it is activating the $timeout promise in the unassociated controller!

But I don't know how to force the controller to $destroy after its unit tests are completed, or how to nuke $timeout to remove/cancel all promises before starting the other unit tests.

As a result, it's causing a conflict between the two unit tests, where one has unexpected behavior that is causing it to fail.

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire