AngularJs Jasmine单元测试嵌套延迟从不调用

时间:2014-08-04 15:59:21

标签: angularjs unit-testing jasmine

我有一个控制器,在发出初始延迟服务请求之后,侦听$ rootScope广播,然后执行另一个延迟服务请求。我为所有服务设置了模拟,但只调用了初始服务。我究竟做错了什么?非常感谢!

控制器:

    .controller('navigationCtrl', function ($scope, $location, $rootScope, $q, $filter,
    sessionService, accountService, activityService, seedService, alertService) {

    var activityMessages = null,
        places = null;

    //process list of unresolved activities and display alert
    function processActivities(activities) {
        angular.forEach(activities, function (activity, index) {
            var placeName = $filter("getById")(places, activity.placeId).label;
            if (!placeName || placeName.length < 1) {
                placeName = "Home";
            }
            var alert = {
                id: "uresolvedActivities-" + activity.id,
                type: "default",
                alertClass: "activity",
                display: {
                    header: "Unresolved Activity at " + placeName,
                    message: activityService.getMessage(activity, activityMessages)
                }
            };
            alertService.addAlert(alert);
        });
    }
    //get dependent data: list of activity messages
    seedService.getActivitiesMessages().then(function (seedData) {
        activityMessages = seedData;
        //watch for sessionRefreshed event
        $scope.$on("draco.sessionRefreshed", function () {
            accountService.getUnresolvedActivityCount().then(function (data) {
                $scope.unviewedCount = data.count;
            });

            var placesPromise = sessionService.get("places"),
                activitiesPromise = accountService.getUnresolvedActivityList();

            $q.all({ places: placesPromise, activities: activitiesPromise }).then(function (data) {
                places = data.places;
                processActivities(data.activities);
            });
        });
    });
});

单元测试:

describe('navigationCtrl', function () {
var $rootScope,
    $scope,
    $filter,
    $location,
    $q,
    mockSessionService,
    mockAccountService,
    mockActivityService,
    mockSeedService,
    mockAlertService,
    activityMessagesDeferred,
    activityCountDeferred,
    activityListDeferred,
    placesDeferred,

    mockAlert = {
        id: "uresolvedActivities-0001",
        type: "default",
        alertClass: "activity",
        display: {
            header: "Unresolved Activity at My Home",
            message: "Hub connection lost"
        }
    },
    mockPlaces = [{ id: "0001", label: "My Home" }],
    mockActivities = [
        {
            created: "20140720", resolved: "20140722",
            type: "dead_gateway", snid: "101", placeId: "0001", viewed: "20140721", state: "open"
        }
    ],
    mockActivityMessages = {
        dead_gateway: {
            open: "gateway dead",
            resolved: "resolved"
        }
    },
    mockMessage = "gateway dead",
    mockActivityCount = 5
;

beforeEach(module('wallyApp'));

beforeEach(inject(function (_$rootScope_, _$filter_, _$q_, _$location_) {
    $rootScope = _$rootScope_;
    $filter = _$filter_;
    $q = _$q_;
    $location = _$location_;
}));

beforeEach(inject(function ($controller) {
    $scope = $rootScope.$new();

    mockAlertService = {
        addAlert: function (alert) {
            return;
        }
    };
    mockSeedService = {
        getActivitiesMessages: function () {
            activityMessagesDeferred = $q.defer();
            return activityMessagesDeferred.promise;
        }
    };
    mockActivityService = {
        getMessage: function () {
            return mockMessage;
        }
    };
    mockAccountService = {
        getUnresolvedActivityCount: function () {
            activityCountDeferred = $q.defer();
            return activityCountDeferred.promise;
        },
        getUnresolvedActivityList: function () {
            activityListDeferred = $q.defer();
            return activityListDeferred.promise;
        }
    };
    mockSessionService = {
        get: function () {
            placesDeferred = $q.defer();
            return placesDeferred.promise;
        }
    };

    spyOn(mockAlertService, 'addAlert').andCallThrough();
    spyOn(mockSeedService, 'getActivitiesMessages').andCallThrough();
    spyOn(mockActivityService, 'getMessage').andCallThrough();
    spyOn(mockAccountService, 'getUnresolvedActivityCount').andCallThrough();
    spyOn(mockAccountService, 'getUnresolvedActivityList').andCallThrough();
    spyOn(mockSessionService, 'get').andCallThrough();

    $controller('navigationCtrl', {
        '$scope': $scope,
        '$filter': $filter,
        '$rootScope': $rootScope,
        '$q': $q,
        '$location': $location,
        'sessionService': mockSessionService,
        'seedService': mockSeedService,
        'accountService': mockAccountService,
        'activityService':mockActivityService,
        'alertService': mockAlertService
    });

}));

describe('listen for sessionService to broadcast session updated', function () {
    //resolve our mock place and session services
    beforeEach(function () {
        //setup

        //resolve promises
        activityMessagesDeferred.resolve(mockActivityMessages);
        placesDeferred.resolve(mockPlaces);
        activityListDeferred.resolve(mockActivities);
        activityCountDeferred.resolve(mockActivityCount);

        //call scope behaivor
        $rootScope.$broadcast("draco.sessionRefreshed");
        $rootScope.$apply();
    });

    //run tests
    it('should call seedService for activity messages', function () {
        expect(mockSeedService.getActivitiesMessages).toHaveBeenCalled();
    });
    it('should call sessionService for places list', function () {
        expect(mockSessionService.get).toHaveBeenCalledWith("places");
    });
    it('should call accountService for unresolved activity count', function () {
        expect(mockAccountService.getUnresolvedActivityCount).toHaveBeenCalled();
    });
    it('should update scope with unresolved activity count', function () {
        expect($scope.unviewedCount).toEqual(mockActivityCount);
    });
    it('should call accountService for unresolved activity list', function () {
        expect(mockAccountService.getUnresolvedActivityList).toHaveBeenCalled();
    });
    it('should call alertService for unresolved activity', function () {
        expect(mockAlertService.addAlert).toHaveBeenCalledWith(mockAlert);
    });
});

});

0 个答案:

没有答案