我目前正在尝试为我的javascript应用创建测试套件。我的问题是,似乎我无法从我的utils对象访问init(),如下所示:
我的应用程序遵循单例模式:
var appModal = function () {
var utils = Object.create(moduleUtils);
function init(caller, options ) {
}
}();
我的测试套件在moduleUtils中,这是一个转换为原型的对象文字
moduleUtils.debug = {
addSlideTest : function(){
/* this function cannot fire init() from appModal */
}}
答案 0 :(得分:1)
这是不可能的。
您需要在公开可见的对象中公开已关闭的函数。
例如,您可以在单元测试中创建一个testMethods
对象来收集私有方法。然后,您的主文件将向对象添加私有方法(如果存在),如下所示:
//In appModal
if (typeof testMethods === "object")
testMethods.init = init;
//In test suite
testMethods = { };
...
testMethods.init();