0

当我在布局控制器的搜索事件上向控制器添加侦听器时,出现此测试错误。

这是侦听器(将其放在工厂中)并在我的控制器中调用它:

function listenToSearchRequest ( viewModel, scope ) {
    scope.$on( 'search:view', function ( event, data ) {
        viewModel.pageDetails.search = data;
        getRequest( viewModel, scope );
    } );
}

我所有的测试都在工作,但自从我尝试收听“search:view”事件后,我不断收到:

TypeError: 'undefined' is not a function (evaluating 'scope.$on')

我的测试的一小部分:

describe( 'User Classification Group Controller', function () {
    var controller;
    var $scope;
    var rs;
    var userClassifications = mockData.getMockPaginatedUserClassifications();

    beforeEach( function () {
        bard.appModule( 'app.admin' );
        bard.inject( '$controller', '$q', '$rootScope', '$log', 'authService', '$httpBackend', '$state', 'toastr', 'FORM_MSGS' );
    } );

    bard.verifyNoOutstandingHttpRequests();

    describe( 'when $scope.$parent is not available', function () {

        beforeEach( function () {
            $rootScope = {};
            $scope     = $rootScope;
            controller = $controller( 'ClassificationGroup', {
                '$scope' : $scope
            } );
        } );

        beforeEach( function () {
            $httpBackend.whenGET( '/api/userclassificationgroups/get_all_paginate/10?page=1&search=&sortField=&sortType=' ).respond( 200, userClassifications );
            $httpBackend.flush();
        } );

        it( 'should not set user classification group count', function () {
            expect( $scope.$parent ).to.equal( undefined );
        } );

    } );
} );

这是抛出的错误:

PhantomJS 1.9.8 (Linux 0.0.0) User Classification Group Controller when $scope.$parent is not available "before each" hook for "should not set user classification group count" FAILED
TypeError: 'undefined' is not a function (evaluating 'scope.$on')

如何模拟 scope.$on 以避免我的所有测试出现此错误?

4

1 回答 1

0

将测试用例代码 $scope 更改为

      $scope = {'$on':function(){}};

为我解决了这个问题。

于 2016-03-30T17:35:38.683 回答