0

I am writing unit test for $interval. The code is like this: angular.module('serviceInvoker', []).

run(function($rootScope, $http, $interval, DateTimeFormatter) {
    $rootScope.loadData = function(serviceName, dataName) {
        $http.get(serviceName)
            .then(function(result) {
                serviceSuccessFunc(result, dataName)
            })
            .catch(function(error) {
                serviceErrorFunc(error, dataName)
            });

        $rootScope.current = moment().toISOString();
        $rootScope.now = moment($rootScope.current).format(DateTimeFormatter.DayHoursFormat); 
    };

    $rootScope.autoRefresh = function(serviceName, dataName, interval) {
        $interval(function() {
            $rootScope.loadData(serviceName, dataName)
        }, interval);
    };

    var serviceSuccessFunc = function(result, dataName) {
        $rootScope[dataName] = result.data;
    };

    var serviceErrorFunc = function(error, dataName) { 
        $rootScope[dataName] = error;
    };
});

The test code is like this:

describe('serviceInvoker', function() {

beforeEach(module('serviceInvoker'));
beforeEach(module(function($provide) {
    $provide.value('DateTimeFormatter', {
        DayHoursFormat: 'HH:mm'
    });

    $provide.value('serviceSuccessFunc', jasmine.createSpy());
    $provide.value('serviceErrorFunc', jasmine.createSpy());
}));
var $interval;
beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) {
    $rootScope = _$rootScope_;
    scope = $rootScope.$new();
    $interval = _$interval_;
    serviceSuccessFunc = _serviceSuccessFunc_;
    serviceErrorFunc = _serviceErrorFunc_;
}));

 describe("loadData function ", function () {

    it("should expose loadData function to $rootScope", function () {
        expect(angular.isFunction($rootScope.loadData)).toBe(true);
    });

    it("should be called", inject(function($http) {
        spyOn($rootScope, 'loadData');
        $rootScope.loadData('service', 'data');
        expect($rootScope.loadData).toHaveBeenCalledWith('service', 'data');
    }));
});

 describe("autoRefresh function ", function () {

    it("should expose autoRefresh function to $rootScope", function () {
        expect(angular.isFunction($rootScope.autoRefresh)).toBe(true);
    });

    it("should be called", function() {
        var $intervalspy = jasmine.createSpy('$interval', $interval); 
        spyOn($rootScope, 'autoRefresh').and.callThrough();

        $rootScope.autoRefresh('service', 'data','interval');
        expect($rootScope.autoRefresh).toHaveBeenCalledWith('service', 'data', 'interval');  

        expect($intervalspy).toHaveBeenCalled();
        // expect($intervalspy).toHaveBeenCalledWith(jasmine.any(Function), 1000);
    });
});

});

But there is an error for interval: Error: Expected spy $interval to have been called.

I don't know how to write unit test for interval which is inside a function(in a run block), can anyone give me some help?

Yannan HE
  • 15
  • 2

1 Answers1

0

$intervalspy isn't used anywhere but the test itself. It isn't called.

As any other tested service, $interval should be spied, mocked or stubbed. It can be spied with a decorator like that, and stubbing is even simpler:

beforeEach(() => {
  module({ $interval: jasmine.createSpy() })
});

...
$rootScope.autoRefresh('service', 'data', 'interval');
expect($interval).toHaveBeenCalledWith(jasmine.any(Function), 'interval');
expect($rootScope.loadData).not.toHaveBeenCalled(), 

var intervalCallback = $interval.calls.first().args[0];
intervalCallback();
expect($rootScope.loadData).toHaveBeenCalledWith(...), 
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • Thank you so much, but I don't understand what is: beforeEach(() => { module({ $interval: jasmine.createSpy() }) }); Where should I define it? I am new to angularjs. Thanks! – Yannan HE Jul 16 '17 at 01:12
  • You should define it in the same place as other beforeEach blocks. – Estus Flask Jul 16 '17 at 01:16
  • like this? beforeEach(inject(function (_$rootScope_, _$interval_, _serviceSuccessFunc_, _serviceErrorFunc_) { $rootScope = _$rootScope_; scope = $rootScope.$new(); $interval = _$interval_; serviceSuccessFunc = _serviceSuccessFunc_; serviceErrorFunc = _serviceErrorFunc_; $interval = jasmine.createSpy(); })); – Yannan HE Jul 16 '17 at 01:17
  • beforeEach blocks with `module` should come before the ones with `inject`. You've already got `beforeEach(module(...))` blocks. You can add another one there. – Estus Flask Jul 16 '17 at 01:28