如何在控制器中测试角度$ destroy

时间:2014-12-22 15:42:05

标签: angularjs angularjs-scope jasmine karma-runner

我有控制器监听destroy事件,我无法弄清楚如何测试这个事件。

任何人都知道如何测试毁灭?

在控制器中:

$scope.$on('$destroy', function () {
    //does something
});

在测试中:

beforeEach(inject(function ($rootScope, $injector) {
    rootScope = $injector.get('$rootScope');
    scope = $rootScope;
}));

it('should do something when scope $destroyed', function () {
    //how to trigger the destroy event listened for in controller?
    expect(somefunction).toHaveBeenCalled();
});

感谢您

1 个答案:

答案 0 :(得分:6)

我没有尝试过,但范围有一个函数$destroy()。在做断言之前调用它。

it('should do something when scope $destroyed', function () {
    //how to trigger the destroy event listened for in controller?
    scope.$destroy();  // created somewhere before each test
    expect(somefunction).toHaveBeenCalled();
});