vendredi 29 mai 2015

Jasmine create a fake object that is globally defined

I have a globally defined function (assume that somewhere in JSP):

var ABC = function (block1, block2) {
// lots of  properties
// lots of functions
}

And also have an angular app and a module:

angular.module('module_name').run(['$rootScope', '$window', function ($rootScope, $window) {
    'use strict';
        $(document).ready(initABC);
    }

    function initABC() {
        $window.ModalWindow = new ABC($('.modal-window'), someOtherParameter);
...
    }

}]);

Code is perfectly working. I want to create a test for it.

ddescribe('Modal directive', function () {
    'use strict';

    var $compile, $rootScope, $window;
    var mod, $scope;

    beforeEach(inject(function (_$compile_, _$rootScope_, _$window_) {
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        $window = _$window_;
    }));

    beforeEach(function () {
        compileDirective('<module_name></module_name>');
    });

    describe('Some functionality', function () {

        it('is not shown when some property is not set', function () {
            expect(mod.find('.modal').length).toBe(0);
        });

    });

    function compileDirective(element) {
        mod = AngularTest.compileDirective($rootScope, element);
        $scope = mod.isolateScope();
        $scope.$digest();
    }
});

When compiling directive initABC method is called, which creates new ABC instance. When running the test I am getting following error:

ReferenceError: Can't find variable: ABC
    at initABC

I guess I have to mock it in my test? How can I do it? I have tried a lot of things including spyOn and jasmine.createSpyObj, but nothing helped (maybe I did in a wrong way).

Aucun commentaire:

Enregistrer un commentaire