如何通过 Jasmine 对 Knockout 和 Durandal 中的本地功能进行单元测试?

问题描述

我有像这样使用 requireJS 的视图模型文件(比如 SOService):

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');
        }
    }
);

现在,在此视图模型的 jasmine 规范文件中,我可以为 func1 和 func2 编写测试,但无法访问 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.
            });
        });
    }
);

那么有什么方法可以测试这个“func3”方法而不将它作为属性添加到 vm 对象中?我也不想用所有本地方法污染我的对象。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)