Search code examples
angularjskarma-mocha

Angular's $interval service not found when testing using karma and mocha


I created a service that has $interval as a dependency and makes use of it and seems to work. Unfortunately when I'm trying to unit test the app $interval service is not found by angular:

Unknown provider: $$qProvider <- $$q <- $interval

I am not calling the service inside a controller as usual, but on the run() method of the app:

app.service('myService', ['$rootScope', '$window', '$interval', myService]);
app.run(function (myService) {
...
});

It works, but if I try to test the app crashes. Rest of angular services don't seem to have this problem ($window, $location, $rootScope, ...) and even this same service works if I attach my service to a controller instead than calling it at app.run():

app.controller('myController', ['myService', function(myService){ ... }]);

I use Karma+Mocha+Sinon+Chai to test.

UPDATE Example with mini app trying to use $interval at app.run():

var anApp = angular.module('myTestApp', ['ngRoute']);
anApp.run(function($rootScope, $timeout, $window, $location, $interval) {
    // blah
});

The test:

describe("Lalarala", function() {
    var scope = null;

    beforeEach(function() {
        module("myTestApp");
        inject(function ($rootScope) {
            scope = $rootScope.$new();
        });
    });

    it("doesnt crash", function () {
        //blah
    });
});

Note: If you remove $interval from the app.run() it works. Instead, other angular services like $timeout, $window or $location don't seem to bother.

Also, I've noticed that other services like $resource have this problem too.

I presume some of those services require something else to be there before they are ready and that's why I can't call them at app.run()?

Thanks for any help.


Solution

  • Ok,

    After checking many things I found the problem had to do with an angular-mocks being out of date.

    I updated versions of angular and angular-mocks and now everything is working fine.

    Sorry!