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

前端 未结 3 983
独厮守ぢ
独厮守ぢ 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:57

    There is an issue in github regarding this: https://github.com/angular/angular.js/issues/14129

    Basically it is working as intended, not calling $onInit or $onChanges automatically.

    it makes no sense (or low sense) to execute $onInit, I explain it: $componentController is to instance controllers a kind of replacement for $controller, but instead of creating instances of controllers registered by the controllerProvider it creates instances of controllers registered through directives (the ones that satisfies a component definition). So, once you have the instance of the controller, you can call manually $onInit, and all the lifecycle of your controller, the idea is that you are testing a controller, not a directive (and its relationships).

提交回复
热议问题