vendredi 3 juillet 2015

AngularJS Jasmine testing init functions

In my controller's init function I route various logic based on stateParams. I want to write unit tests against this scenarios but I'm having trouble getting it working.

My init function

    var _init = function () {
        // Get full companyList
        servicecall().then(function (response) {

            if ($stateParams.ID) {
                $scope.callOne();
            }
            else if ($stateParams.Index == -1) {
                $scope.callTwo();
            }
            else if ($stateParams.Index == '' || $stateParams.Index == null) {
                $scope.callThree();
            }
            else
                $scope.callFour();
        },
        function (err) {
            console.log(err);
        });
    };
    _init();

So simply I want to set $stateParams.Index = -1, and make sure callTwo() gets called.

My beforeEach looks like

    beforeEach(function () {

        controller = $controller('Controller', {
            $scope: $scope,
            $stateParams: $stateParams,
            $state: $state
        });

        // And add $scope's spyOns
        spyOn($scope, 'callOne').and.callThrough();
        spyOn($scope, 'callTwo').and.callThrough();
        spyOn($scope, 'callThree').and.callThrough();
        spyOn($scope, 'callFour').and.callThrough();
    });

At first I tried the below, which of course did not work; it says spy was not called.

        it("should call callTwo if stateParams.index is -1", function () {
            $stateParams.Index = -1;
            expect($scope.callTwo()).toHaveBeenCalled();
        });

I figured that all of the init was happening before my spy attached, so then I tried moving that stuff inside of my it call but everything broke down.

This one says callTwo has already been spied upon.

    it("should call callTwo if stateParams is -1", function () {
            $stateParams.Index = -1;
            spyOn($scope, 'callTwo').and.callThrough();

            controller = $controller('Controller', {
                $scope: $scope,
                $stateParams: $stateParams,
                $state: $state
            });

            expect($scope.callTwo).toHaveBeenCalled();
        });

But if I move the spy declaration after the controller is initialized it says it's not called again

  1. How can I ensure calls are being made as expected during a controller instantiation?

Aucun commentaire:

Enregistrer un commentaire