Javascript 单元测试.使用$componentController的组件-特别是$postLink

Javascript 单元测试.使用$componentController的组件-特别是$postLink,javascript,angularjs,unit-testing,angular-components,Javascript,Angularjs,Unit Testing,Angular Components,在单元测试中手动触发的$postLink方法中,未定义绑定中的vm/$ctrl值 元件规格 angular.module('softwearplayApp') .component('swImageEditor', { templateUrl: 'views/sw-image-editor.html', bindings: { image: '<', close: '&', mode: '<', save:

在单元测试中手动触发的$postLink方法中,未定义绑定中的vm/$ctrl值

元件规格

angular.module('softwearplayApp')
.component('swImageEditor', {
    templateUrl: 'views/sw-image-editor.html',
    bindings: {
      image: '<',
      close: '&',
      mode: '<',
      save: '&'
    },
    controller: SwImageEditorController
  });
angular.module('softwearplayApp')
.component('swImageEditor'{
templateUrl:'views/sw image editor.html',
绑定:{

图片:'我不明白为什么它应该是未定义的。你到底是如何确保它在一个地方定义,在另一个地方未定义的?什么是控制台输出?一个可以复制这个问题的plunk/fiddle会有帮助。我认为绑定“this”时有一个bug调用flush方法时$timeout回调中的值。它在应用程序中的实际回调中有效,但在ngMocks中无效。我会看看是否可以设置一个fiddle。工作链接添加到top@estus
$ctrl.$postLink = function() {
  console.log($ctrl.image); //<----------- $ctrl.image is defined
  $timeout(function() {
    var img = new Image();

    img.src = $ctrl.image.url; //<--------- $ctrl.image is undefined
    img.classList.toggle('hidden');

    imageContainer = $element[0].querySelector('.image-editor__container');
    cropResultContainer = $element[0].querySelector('.image-editor__crop-result');

    imageContainer.append(img);

    cropper = new Cropper(img, {
      aspectRatio: ASPECT_RATIO[$ctrl.mode]['4/3'],
      ready: function() {
        $ctrl.isLoading = false;
      }
    });
  });
};
var
    $componentController,
    $timeout,
    $ctrl,
    $rootScope = {},
    LOCALS = {
      $element: {}
    },
    BINDINGS = {
      image: {url: 'abcdef'},
      close: function() {},
      mode: 'landscape',
      save: function() {
      }
    },
    ASPECT_RATIO;

  beforeEach(inject(function(_$rootScope_, $compile, $templateCache, _$componentController_, _ASPECT_RATIO_, _$timeout_) {
    ASPECT_RATIO = _ASPECT_RATIO_;
    $timeout = _$timeout_;
    $componentController = _$componentController_;
    $rootScope = _$rootScope_;
    LOCALS.$scope = $rootScope.$new();
    LOCALS.$element = angular.element('<sw-image-editor></sw-image-editor>');
    LOCALS.$element = $compile(LOCALS.$element)(LOCALS.$scope);
  }));

    it('should define the $postLink', function() {
     $ctrl = $componentController('swImageEditor', LOCALS, BINDINGS);
      $ctrl.$postLink();
      $timeout.flush();
      expect($ctrl.showCroppedImage).toBeTruthy();
      expect($ctrl.isLoading).toBeFalsy();
    });

  });