测试来自服务的广播,从控制器调用

时间:2014-08-11 16:42:41

标签: angularjs jasmine

在我的Angular控制器中,我调用服务以将多个事件附加到网格。例如,我将onXLE事件附加到网格中,如下所示:

gridService.attachEvent("visibilityGrid", "onXLE", function () {
        $rootScope.$broadcast("loader_hide");
    });

但我还附加(使用与服务相同的功能)其他事件,例如onCheckonDynXLS。我遇到的问题是,我似乎无法检查$broadcasts是否正在解雇。我尝试了通常的做法:

it( 'should attach the onXLE event to the grid',function() {
   spyOn( gridService,'attachEvent' );
   spyOn( $rootScope,'$broadcast' );

   // First test passes
   expect( gridService.attachEvent ).toBeDefined();   

   // This fails...
   expect( $rootScope.$broadcast ).toHaveBeenCalledWith( 'loader_hide' );   

});

它返回此错误:

Expected spy $broadcast to have been called with [ 'loader_hide' ] but it was never called. 

我缺少什么让这项测试工作?

更新

$locationChangeStart的调用发生在此处:

// For any route changes, check if roles have already been obtained, if not, then retrieve them.
    $scope.$on('$locationChangeStart', function(event, newUrl, oldUrl){

        messageService.clearAllMessages("messageContainer");

        if(!authService.isInitialized()) {

            if(authService.isDevMode()){

                dataService.getRoles(AUTH_APPLICATION_ID, function(response){
                    $scope.root.initAuthorization(response);
                });

            } else {

                dataService.retrieveSMHeaders()
                    .success(function(data, status, headers, config) {

                        authService.setEmployeeId(headers('EMPLOYEE_ID'));
                        authService.setEmployeeName(headers('USER_NAME'));
                        authService.setEmployeeSurname(headers('USER_SURNAME'));

                        // Set the Headers required to access Common Services.
                        $http.defaults.headers.common.SERVICES_ID = AUTH_SERVICES;

                        dataService.getRoles(AUTH_APPLICATION_ID, 
                            function(response){
                                $scope.root.initAuthorization(response);
                            },
                            function(status){
                                event.preventDefault();
                                $location.url("/unauthorized");
                            }
                        );

                    });
            }
        }

        authService.getAuthPromise().then(function(){

            var isAuthorized = false;
            var userRoles = $route.current.$$route.roles;
            if(userRoles){
                var userRoleArray = userRoles.split(',');
                for (x=0; x<userRoleArray.length; x++){
                    if(authService.hasRole(userRoleArray[x])){
                        isAuthorized = true;
                        break;
                    }
                }
            } else {
                isAuthorized = true;
            }

            if (!isAuthorized) {
                event.preventDefault();
                $location.url("/unauthorized");
            }
        });
    });

上述观点是确定用户是否具有执行操作的正确角色 - 如果没有获得它们

1 个答案:

答案 0 :(得分:2)

事件在摘要周期内广播(和发出),在测试中,您需要通过调用$rootScope.$apply()

来自行启动
it( 'should attach the onXLE event to the grid',function() {
   spyOn( gridService,'attachEvent' );
   spyOn( $rootScope,'$broadcast' );

   // First test passes
   expect( gridService.attachEvent ).toBeDefined();   

   // this is what you are missing
   $rootScope.$apply();

   // This fails...
   expect( $rootScope.$broadcast ).toHaveBeenCalledWith( 'loader_hide' );   
});

注意:在解决承诺时也是如此。