How can I test events in angular?

前端 未结 3 643
谎友^
谎友^ 2020-11-28 04:57

I need to test that events get correctly emitted or broadcast, and trigger events manually.

What\'s the best way to do this?

3条回答
  •  孤独总比滥情好
    2020-11-28 05:33

    Here are the steps you should follow to $broadcast event in angular JS

    While inijecting initialize the rootScope and scope stub as given below:

    var rootScope;
    var scopeStub = beforeEach(function() {
        inject(function($rootScope, _$controller_) {
            rootScope = $rootScope;
            scopeStub = $rootScope.$new();
            $controller = _$controller_;
        });
    });
    

    After controller is created raise event using rootScope like below:

    rootScope.$broadcast('eventName', parameter1);
    

提交回复
热议问题