How to use $rootScope. $on?

Asked

Viewed 338 times

1

I don’t know how to set $rootScope. $on on my unit test. Whenever I invoke a function it gives me the following error:

Typeerror: Attempted to assign to readonly Property.

The test is being done as follows:

describe('Testing EventHandler', function () {
 beforeEach(module('app'));

 var srv, rootScope, injector, httpBackend, animate, scope, location, loginService ;

 beforeEach(inject(function (EventHandler,_$injector_, $rootScope, $httpBackend, $animate, $location, LoginService) {
  injector = _$injector_;
  srv = EventHandler;
  rootScope = $rootScope;        
  animate = $animate;
  location = $location;
  loginService = LoginService;

}));    

 it('testing if the initialize_component function is defined', function(){
    expect(srv.initialize).toBeDefined();
    spyOn(srv, 'initialize').and.callThrough();
    srv.initialize({$animate: animate});
}); 

Of the function present in the eventHandler.service.js

(function () {
'use strict';

angular.module("app")

    .service('EventHandler', EventHandler);

EventHandler.$inject = ['$rootScope', '$location', '$animate', '$state', '$stateParams', '$sanitize', '$q', '$uibModal', 'toastr',
    'storage', 'StorageService', 'LogService', 'LanguageResource', 'LoginService', 'StcpUtilsService', 'DAOService',
    'CSRF', 'Authorization', 'ShortcutService', 'data_login', 'PermissionsService', 'BusinessLogicService', 'DebugMode'
];

/**
 * Método que instancia o serviço.
 * @param $rootScope serviço de acesso ao escopo raiz.
 * @param $location serviço de redirecionamento ao utilizar rotas por url.
 * @param $animate serviço de animações AngularJS atraves de CSS.
 * @param $state serviço de estados.
 * @param $stateParams serviço auxiliar para manipulação de estados.
 * @param $sanitize serviço de sanitização de inputs.
 * @param $q serviço de promises.
 * @param $uibModal serviço de modal bootstrap.
 * @param toastr serviço de exibição de mensagens temporárias.
 * @param storage value to set StorageServiceUsage.
 * @param StorageService serviço de armazenamento temporário em browser.
 * @param LogService serviço de exibição de logs simplificado.
 * @param LanguageResource serviço de tradução de idiomas.
 * @param LoginService serviço de login e logout.
 * @param StcpUtilsService serviço de utilidade em views.
 * @param DAOService serviço de acesso a objeto de persistência e consulta de dados via STCPRest.
 * @param CSRF constant de checagem de segurança de aplicações web (Cross-site request forgery).
 * @param Authorization
 * @param ShortcutService serviço para disparo de teclas de atalho.
 * @param data_login
 * @param PermissionsService serviço de checagem de permissões de ações do operador.
 * @param BusinessLogicService serviço de apoio a regras de negócios de cada entidade.
 * @returns {{initialize: initialize_component, onCheckSession: onCheckSessionHandle, onLogin: onLoginHandle, onLogout: onLogoutHandle, onStateChangeError: onStateChangeErrorHandle, onHideMenuCheck: onHideMenuCheckHandle, onSetInstance: onSetInstanceHandle, onGetInstanceName: onGetInstanceNameHandle, onGetInstanceId: onGetInstanceIdHandle, onGetOperatorLogged: onGetOperatorLoggedHandle, onOperatorIsLogged: onOperatorIsLoggedHandle, onShakeMenu: onShakeMenuHandle, onCheckInstance: onCheckInstanceHandle, onSave: onSaveHandle, onDelete: onDeleteHandle}}
 * @constructor
 */
function EventHandler($rootScope, $location, $animate, $state, $stateParams, $sanitize, $q, $uibModal, toastr, storage, StorageService,
                      LogService, LanguageResource, LoginService, StcpUtilsService, DAOService, CSRF,
                      Authorization, ShortcutService, data_login, PermissionsService, BusinessLogicService, Debug) {

    var animate;
    /* = parameters.$animate || $animate; */
    var rootScope;
    /* = parameters.$rootScope || $rootScope; */
    var scope;
    /* = parameters.$scope; */
    var _toastr;
    /* = parameters.toastr || toastr; */
    var _storage;
    /* = parameters.StorageService || StorageService; */
    var location;
    /* = parameters.$location || $location; */
    var log;
    /* = parameters.LogService ? parameters.LogService.log : LogService.log || LogService.log; */
    var language;
    /* = parameters.LanguageResource || LanguageResource; */
    var shortcut;
    var t;
    var dao;
    var DebugMode;

    return {
        initialize: initialize_component,
        onCheckSession: onCheckSessionHandle,
        onLogin: onLoginHandle,
        onLogout: onLogoutHandle,
        onStateChangeError: onStateChangeErrorHandle,
        onHideMenuCheck: onHideMenuCheckHandle,
        onSetInstance: onSetInstanceHandle,
        onRemoveInstance: onRemoveInstanceHandle,
        onGetInstanceName: onGetInstanceNameHandle,
        onGetInstanceId: onGetInstanceIdHandle,
        onGetOperatorLogged: onGetOperatorLoggedHandle,
        onOperatorIsLogged: onOperatorIsLoggedHandle,
        onShakeMenu: onShakeMenuHandle,
        onCheckInstance: onCheckInstanceHandle,
        onSave: onSaveHandle,
        onDelete: onDeleteHandle,
        onInterceptError: onInterceptErrorHandle,
        onChangeOperatorPassword: onChangePasswordHandle,
        onGetTransfer: onGetTransferHandle,
        onOpenChangeModuleDialog: onOpenChangeModuleDialogHandle,
        onChangeSubModule: onChangeSubModuleHandle,
        onDisplaySelectionInstance: onDisplaySelectionInstanceHandle,
        onGetInstances: onGetInstancesHandle,
        onCheckModule: onCheckModuleHandle,
        // onDataToPersist: onDataToPersistHandle
    };

 /**
     * Método que inicializa os componentes essenciais do Serviço.
     * @param parameters objeto contendo os Services/Factories/Methods/Vars a serem utilizados pelo serviço.
     */
    function initialize_component(parameters) {
        animate = parameters.$animate || $animate;
        rootScope = parameters.$rootScope || $rootScope;
        scope = parameters.$scope;
        _toastr = toastr;
        _storage = StorageService;
        location = $location;
        log = LogService.log;
        language = parameters.LanguageResource || LanguageResource;
        language.init();
        language.updateLang();
        t = language.t;
        shortcut = ShortcutService.shortcut;
        dao = DAOService;
        DebugMode = Debug;
        getFormatDate();
        check_module();
        onLoginHandle();
        onLogoutHandle();
        check_instance();
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.