How to unit test local functions in Knockout and Durandal through Jasmine?

138 views Asked by At

I have my View Model file(say SOService) using requireJS like so:

define(['dependencies/dep1'],
    function (dep1) {
        var vm = {
            func1: func1,
            func2: func2
        };
        return vm;

        function func1() {
            vm.func2();
        }
        function func2() {
            alert('Hi');
        }
        function func3() {
            alert('Hello');
        }
    }
);

Now, in my jasmine spec file for this view model, I can write tests for func1 and func2 but I cannot access func3.

define(['app/SOService'],
    function (SoService) {
        describe('SoService', function () {
            "use strict";
            describe('on executing func1', function () {
                it('should call func2', function () {
                spyOn(SoService, 'func2');

                SoService.func1();

                expect(SoService.func2).toHaveBeenCalled();
            });
            describe('on executing func2', function () {
                it('should alert Hi', function () {
                spyOn(window, 'alert');

                SoService.func2();

                expect(window.alert).toHaveBeenCalledWith('Hi');
            });

            //But testing func3 is not possible without adding it as a property to the vm object.
            describe('on executing func3', function () {
                it('should alert Hello', function () {
                //spyOn(SoService, 'func3');
                //spyOn(window, 'func3'); // Will not work since it does not exist on window nor SoService

                SoService.func3(); //Cannot call this since it does not exist on SoService object.
            });
        });
    }
);

So is there any way I could test this 'func3' method without adding it as a property to the vm object? I don't want to pollute my object with all the local methods as well.

0

There are 0 answers