How to test on destroy scope

后端 未结 4 863
刺人心
刺人心 2021-02-13 19:02

How to unit testing an $destroy event of a Directive in angularjs?

I have the code in my directive:

scope.$on(\'$destroy\', function () {
    //clean som         


        
4条回答
  •  深忆病人
    2021-02-13 19:15

    Here is what I do:

    var destroyed = spyOn(scope, '$destroy').and.callThrough();
    scope.$destroy();
    expect(destroyed).toHaveBeenCalled();
    

    unlike other answers I don't have to create flag variables that only make sense for testing, also, it makes more sense to me to use Jasmine spyOn and callThrough to check if the function $destry is being successfully called.

提交回复
热议问题