茉莉花无法识别角度事件监听器$ on

Son*_*nne 2 jasmine angularjs karma-runner

我添加了事件广播和发射,现在茉莉花无法加载元素。

控制器功能,谁接收事件,我想测试看起来像这样

    function Tree(scope, $http, dataservice, Block, events) {

    var $scope = scope;
    init();
    function init(){
        // React to global events
        $scope.$on(events.reloadOnNewData, onNewData);
    }
Run Code Online (Sandbox Code Playgroud)

在Jasmine中,我在单元测试中有以下代码:

beforeEach(inject(function (_$controller_, _ParameterList_) {
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
    controller = $controller('Tree', {$scope: $scope});
}));
Run Code Online (Sandbox Code Playgroud)

在因果报应中,我使用流动的角度文件

'./bower_components/jquery/dist/jquery.js',
        './bower_components/angular/angular.js',
        './bower_components/angular-mocks/angular-mocks.js',
        './bower_components/angular-animate/angular-animate.js',
        './bower_components/angular-route/angular-route.js',
        './bower_components/angular-sanitize/angular-sanitize.js',
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我得到

    TypeError: $scope.$on is not a function
Run Code Online (Sandbox Code Playgroud)

ngL*_*ver 5

您需要实例化rootScope并将其传递给控制器​​,然后它不会显示此错误。

  var $scope;
    beforeEach(inject(function (_$controller_, _ParameterList_,$rootScope) {
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $scope =  $rootScope.$new();
        $controller = _$controller_;
        controller = $controller('Tree', {$scope: $scope});
    }));
Run Code Online (Sandbox Code Playgroud)