Errors (forEach @, loadModules @, createInjector @, workFn @) when testing $ controller

I decorated the controller "originalCtrl" by decorating the $ controller service:

//module declaration
angular.module('decorationModule', [
        'originalModule',
        'ExternalService'
    ])
    //decoration
    .decorator('$controller', function ($delegate, ExternalService) {
        return function () {
            //check when the OriginalCtrl is instantiated
            if (arguments[0] === 'OriginalCtrl') {
                return function () {
                    const ctrl = $delegate.apply($delegate, arguments)();
                    //memorize the original close function
                    const delegatedCloseFn = ctrl.closefn;
                    ctrl.close = function close() {
                        alert('bye bye before closing');
                        ExternalService.fn();
                        delegatedCloseFn();
                        alert('bye bye After closing');
                    };
                    return ctrl;
                }
            }
            else {
                return $delegate.apply($delegate, arguments);
            }
        }
    });

It works well.

But trying to do a Unit test on it:

beforeEach(angular.mock.module('decorationModule', ($controller, $q, ExternalService) => {
    scope = $rootScope.$new();

    createController = () => {
        return $controller('OriginalCtrl', {$scope: scope});
    };
    spyOn(ExternalService, 'fn').and.returnValue();
}));

it('should call decorated close function', inject((ExternalService) => {
    //given
    const ctrl = createController();
    expect(ExternalService.fn).not.toHaveBeenCalled();

    //when
    ctrl.close();
    //then
    expect(ExternalService.fn).toHaveBeenCalled();
}));

There was an error:

Foreach @ / home /.../ bower_components / angular / angular.js: 322: 24 loadModules @ / home /.../ bower_components / angular / angular.js: 4548: 12 createInjector @ / home /.../ bower_components /angular/angular.js:4470:30 workFn @ / home /.../ bower_components / angular -mocks / angular -mocks.js: 2954: 60

+4
source share

All Articles