Angular Testing: Spy a function that was executed on the initialize of a controller

旧巷老猫 提交于 2019-12-05 16:04:08

问题


I've been trying to spy a function that was executed on the initialize of a controller, but the test always failed. I've been trying execute $scope.$digest() and this it's not working, However in the console, i see that the function have been called.

I can't figure out this, Someone can explain to me why this it's not working?

Codepen Example: http://codepen.io/gpincheiraa/pen/KzZNby

Controller

function Controller($stateParams, $scope){

  $scope.requestAuthorization = requestAuthorization;

  if ($stateParams.requestAuthorization === true) {
    console.log('$stateParams.requestAuthorization');
    $scope.requestAuthorization();
  }
  function requestAuthorization() {
    console.log('requestAuthorization()');
  }
}

Testing

describe('AppCtrl', function(){
     var AppCtrl, $rootScope, $scope, $stateParams;

    beforeEach(module('exampleApp'));

    beforeEach(inject(function($controller, _$rootScope_, _$injector_, _$stateParams_) {
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        $stateParams = _$stateParams_;
        $stateParams.requestAuthorization = true;


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

        spyOn($scope, 'requestAuthorization');          
    }));

     it('$stateParams.requestAuthorization should be defined', function() {             
        expect($stateParams.requestAuthorization).toBeDefined();
    });

    it('$scope.requestAuthorization should be defined', function() {
        expect($scope.requestAuthorization).toBeDefined();
    });

    // this test is not passing.. 
    it('should call requestAuthorization', function() {
                //$scope.$digest();
        expect($scope.requestAuthorization).toHaveBeenCalled();
    });

});

回答1:


Your test is failing because spy gets overridden by real function when controller initializes. One way to avoid this is monkey-patching $scope object with custom setter for requestAuthorization property, that could create spy when controller is trying to assign value to this property:

    beforeEach(inject(function($controller, _$rootScope_, _$injector_, _$stateParams_) {
        $rootScope = _$rootScope_;
        $scope = $rootScope.$new();
        var reqAuthSpy;
        Object.defineProperty($scope, 'requestAuthorization', {
            get: function() {return reqAuthSpy;},
            set: function(fn) {
             reqAuthSpy = jasmine.createSpy('reqAuthSpy');
            }
        });
        $stateParams = _$stateParams_;
        $stateParams.requestAuthorization = true;


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

    }));


来源:https://stackoverflow.com/questions/36503406/angular-testing-spy-a-function-that-was-executed-on-the-initialize-of-a-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!