How to trigger $onInit or $onChanges implictly in unit testing Angular component controller?

前端 未结 3 1094
独厮守ぢ
独厮守ぢ 2021-02-13 04:20

I\'m using Angular 1.5.5 and Jasmine as test framework. Currently I have to do something like this so that the test passes:

function createController(bindings) {         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 04:45

    I don't know if this will help, but for testing components I do the following

    beforeEach(module('templates'));
    
    var element;        
    var scope;
    
    beforeEach(inject(function ($rootScope, $compile) {
        scope = $rootScope.$new();
        scope.draw = new ol.source.Vector({ wrapX: false });
        element = angular.element('');
        element = $compile(element)(scope);
    }));
    
    var controller;
    beforeEach(inject(function ($rootScope, $componentController) {
        scope = $rootScope.$new();
        scope.draw = new ol.source.Vector({ wrapX: false });
        controller = $componentController('customElement', {draw: new ol.source.Vector({ wrapX: false })}, { $scope: scope });
    }));
    

    and $onInit() and $onChanges() get triggered when they should be, by themselves

提交回复
热议问题