0

I want to be able to do something like:

expect($interval).toHaveBeenCalledWith(jasmine.any(Function), correctInterval)

However, I can't figure out how to get hold of $interval to spy on it. Any examples I've seen have been for use with $controller, so you can inject whatever you want into the controller, including a mock of $interval. I don't want to use a mock for every test, just for some tests, so I don't think I can use the $provide syntax in beforeEach to just replace $interval with a raw spy.

Amy Blankenship
  • 6,485
  • 2
  • 22
  • 45
  • You need to apply this only to `beforeEach`s that belong to those `describe` which need to mock `$interval`. `describe` hierarchy should be built accordingly. – Estus Flask Aug 08 '16 at 23:49
  • My understanding is you can only update the provider as angular is bootstrapped, not later. – Amy Blankenship Aug 09 '16 at 14:59
  • That's correct. Bootstrapping happens when `inject()` is called the first time in current spec (`inject` callback is executed during run phase). `module()` callbacks are executed during config phase, `$interval` should be mocked there. The mock can be even moved to separate module for this purpose. – Estus Flask Aug 09 '16 at 15:17
  • I ultimately decided it just wasn't worth it in this instance. There's already a bunch of stuff in that first block and this would make that block harder to read for just one test. MPO is that the angular team went for "clever" in this instance, when if they'd stuck with more conventional syntax this wouldn't even be an issue. – Amy Blankenship Aug 09 '16 at 20:39
  • 1
    Actually, spying for $timeout/$interval is a good practice because otherwise they stay blackboxes in tests. I moved `$timeout` decorator from the link above into a separate Angular module, so spying is always as simple as `beforeEach(module('timeoutSpy'))`. – Estus Flask Aug 09 '16 at 20:48

0 Answers0