角度测试:窥探在控制器初始化时执行的函数 [英] Angular Testing: Spy a function that was executed on the initialize of a controller

查看:51
本文介绍了角度测试:窥探在控制器初始化时执行的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图窥探在控制器初始化时执行的函数,但测试总是失败。
我一直在尝试执行 $ scope。$ digest()并且这不起作用,但在控制台中,我看到该函数已被调用。

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示例:
http://codepen.io/gpincheiraa/pen/KzZNby

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

控制器

function Controller($stateParams, $scope){

  $scope.requestAuthorization = requestAuthorization;

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

测试

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();
    });

});


推荐答案

您的测试失败,因为间谍被实际功能覆盖当控制器初始化时。避免这种情况的一种方法是使用 requestAuthorization 属性的自定义setter进行猴子修补 $ scope 对象,这可能会创建间谍时控制器正在尝试为此属性赋值:

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
        });

    }));

这篇关于角度测试:窥探在控制器初始化时执行的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆