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

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

问题描述

我正在使用 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 沙盒单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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