Javascript Sandbox单元测试 [英] Javascript Sandbox unit testing

查看:109
本文介绍了Javascript Sandbox单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QUnit,这非常棒。

I am using QUnit, which is excellent.

我已将我的JS应用程序包含在(function(){})()中; 沙箱。这隐藏了许多我不想公开的代码,但我还需要测试该代码。

I have enclosed my JS app in the (function () {})(); sandbox. This hides a lot of code that I don't want public, but I also need to test that code.

以下是一个如何工作的示例:

Here is an example of how this works:

(function () {  

    var PublicAPI = window.PublicAPI = {};        
    PublicAPI.publicFunction = function (foo) {
        PrivateAPI.privateFunction(foo);
        return 'bar';
    };

    var PrivateAPI = {};
    PrivateAPI.privateFunction: function (foo) {
       // Make secret stuff that never gets returned to the public
       // Could be an AJAX call.
    }

})();

所以在这里我可以轻松地进行单元测试 PublicAPI.publicFunction ,但我如何测试 PrivateAPI.privateFunction

So here I can easily unit test PublicAPI.publicFunction, but how will I test PrivateAPI.privateFunction ?

推荐答案

这个类似的问题总结得很好......最简单的是不处理私有方法,因为他们可以根据需要改变...公共方法是需要测试的方法。如果你想测试你的内部函数,你需要留下某种钩子以便qunit能够找到。

This similar question sums it up pretty well... The easiest is to not deal with the private methods, as they can change if they want... The public methods are the ones that need testing. If you want to test your internal functions you need to leave a hook of some sort for qunit to be able to find.

这篇关于Javascript Sandbox单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆