mercredi 5 août 2015

Mocking a promise within $provide

I have a $provide block for my AngularJS unit tests. The promises are not working. I am having to do this as my mapService service depends on Google Maps hence uiGmapIsReady, this is the bit I am trying to mock.

var deferredForMock;

module(function ($provide) {
    $provide.value('$window', $window);
    $provide.value('currentUser', currentUser);
    $provide.value('uiGmapIsReady', {
        promise: function () {
            return deferredForMock.promise;
        }
    });
});

// perform injections
inject(function ($rootScope, $controller, $httpBackend, $q, _mapService_) {
    deferredForMock = $q.defer();
    ...

When the mapService tries to use uiGmapIsReady I get the following error:

Cannot read property 'promise' of undefined

This refers to the line in my $provide function where it returns deferredForMock.promise;

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire