Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在AngularJS中进行指令单元测试时访问局部变量_Angularjs_Unit Testing_Angularjs Directive_Karma Runner_Local Variables - Fatal编程技术网

在AngularJS中进行指令单元测试时访问局部变量

在AngularJS中进行指令单元测试时访问局部变量,angularjs,unit-testing,angularjs-directive,karma-runner,local-variables,Angularjs,Unit Testing,Angularjs Directive,Karma Runner,Local Variables,我需要测试一些指令,然后得到它的一个局部变量来检查它的值。指令本身看起来是这样的: angular.module('app').directive('favoritesList', function () { return { restrict: 'E', replace: true, templateUrl: 'templates/directives/favoritesListDirective.html', controller: function ($

我需要测试一些指令,然后得到它的一个局部变量来检查它的值。指令本身看起来是这样的:

angular.module('app').directive('favoritesList', function () {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'templates/directives/favoritesListDirective.html',
    controller: function ($scope, $rootScope, $rootElement, $filter, ContactService, ContactFollowerService, Restangular) {

      var listLimit = 100, // in order to prevent favoritesList *overflow*
          followedFilter = {filters: {followed: true}, page_size:listLimit};

      // get Favorites List
      ContactService.provider.getList( followedFilter ) //
        .then(function(result){
          $scope.favorites=result;
        }, function(message){
          console.warn('Cannot get a Favorites List',message);
        });

      ....


    }
  };
});

那么,我如何才能访问描述/it块中的followedFilter变量?

我建议断开控制器,以便您能够正确地抓取它。虽然可以将控制器与指令断开,但这可能非常困难-最简单的方法是让控制器独立。例如:

(function() {
  'use strict';

  describe('FavouritesListCtrl', function() {

    var $rootScope, $scope, ctrl;
    beforeEach(module('app'));

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

        ctrl = $controller('FavouritesListCtrl',{$scope: $scope});
    }));

    describe('FavouritesListCtrl', function() {
        it('should be defined', function() {
            expect(ctrl).toBeDefined();
        });
    });

  });
});
基于此测试,您希望控制器的定义如下:

(function() {
  'use strict';

  angular
    .module('app')
    .component('favoritesList', {
      bindings: {},
      controllerAs: "vm",
      controller: "FavouritesListCtrl",
      templateUrl: 'example.html'
    });
}());
在这里,我使用的是组件,而不是指令,但它们之间的关系非常密切。如果要测试
以下过滤器
,例如在初始化中:

describe('init', function() {
  it('followedFilter should be set to ', function() {
    expect(ctrl.followedFilter).toBeDefined();
  });
});
这将期望控制器中的
followedFilter
定义如下:

function FavouritesListCtrl() {
  var vm = this;
  vm.followedFilter = {
    //values
  };
}

你不能用这种方式写。见下面我的答案