vendredi 21 août 2015

Jasmine Testing Angular Intervals

I have a controller that uses an interval to initialize some variables.

var availableAppsInterval = $interval(function () {
        if (availableApps === undefined) {
            availableApps = AppFactory.AvailableApps();
            $scope.count = $scope.count++;
        }
        else {
            $interval.cancel(availableAppsInterval);
            $scope.menuUrl = AppFactory.App().menuPath;
            $scope.isOutageAvailable = function () {
                for (each App in availableApps) {
                    if (availableApps[eachApp].name == "Outage")
                        return true;
                }
                return false;
            };

            $scope.count = $scope.count++;
        }
    }, 500);

When I try to unit test using Jasmine, I got the impression that $interval.flush() is not flushing this interval, and I don't know why.

describe("MyController tests -> ", function () {

beforeEach(function () {
    module('MyApp');

    inject(function ($controller, $rootScope, $interval) {

        scope = $rootScope.$new();
        interval = $interval;

        MyController = $controller("MyController", {
            $scope: scope,
            $interval: interval,
        });
    });
});


it('should increase the count after 500 milliseconds', function () {
    expect(scope.menuUrl).toBeUndefined();
    expect(scope.isOutageAvailable).toBeUndefined();
    expect(scope.count).toBe(0);

    interval.flush(1000);
    expect(scope.count).toBeGreaterThan(0);
});

});

So far I am just testing if the interval is working. The entire application is working properly, so I know the interval works. But the test is failing saying Expected 0 to be grater than 0 meaning the count did not increase.

Your help is very appreciated.

Aucun commentaire:

Enregistrer un commentaire