2017-06-16 6 views
0

私は、コンポーネントのコントローラを模擬しています。ここでは、angularjsの$controllerProviderを使用してコントローラを模擬しています。注入は正常に機能していますが、コントローラのコンテキストthisは登録中でもundefinedです。コントローラは模擬した後でもユニットテストを定義していません

マイコンポーネント

import templateUrl from './pdf-export-modal.html.pug'; 
import './pdf-export-modal.less'; 

export default { 
    templateUrl, 
    bindings: { 
    onClose: '&', 
    }, 
    controller: 'PdfExportModalCtrl', 
}; 

と問題がES6の自動バインディングとしたコンポーネントのテストファイル

import angular from 'angular'; 

describe('component: pdf-export-modal',() => { 
    let $rootScope; 
    let $compile; 
    let scope; 
    let el; 
    let html; 
    let ctrl; 

    beforeEach(angular.mock.module('mainModule', ($controllerProvider) => { 
    $controllerProvider.register('PdfExportModalCtrl',() => { 
     ctrl = this; 
     console.log(ctrl, this); // both are undefined here 
    }); 
    })); 

    beforeEach(inject((_$rootScope_, _$compile_) => { 
    $rootScope = _$rootScope_; 
    $compile = _$compile_; 
    scope = $rootScope.$new(); 
    html = "<pdf-export-modal on-close='onClose'/>"; 
    scope.onClose = jasmine.createSpy('onClose'); 
    el = $compile(html)(scope); 
    scope.$apply(); 
    })); 

    describe('selecting compact',() => { 
    // test 
    }); 
}); 

答えて

0

。矢印関数は、テストクラスのthis文脈を使用してinstead of creating a new onePdfModalCtrlを使用しました。矢印機能を削除し、ES5構文を使用して機能させました。

beforeEach(angular.mock.module('mainModule', ($controllerProvider) => { 
    $controllerProvider.register('PdfExportModalCtrl', function() { 
    ctrl = this; 
    console.log(ctrl); //new context is created for the controller  
}); 
})); 
関連する問題