Javascript中的白盒测试 - 如何处理隐私? [英] White-box testing in Javascript - how to deal with privacy?

查看:114
本文介绍了Javascript中的白盒测试 - 如何处理隐私?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个小型Javascript应用程序中的模块编写单元测试。为了保持界面清洁,一些实现细节由匿名函数(通常用于隐私的JS模式)关闭。但是,在测试时我需要访问/模拟/验证私有部分。

I'm writing unit tests for a module in a small Javascript application. In order to keep the interface clean, some of the implementation details are closed over by an anonymous function (the usual JS pattern for privacy). However, while testing I need to access/mock/verify the private parts.

我之前编写的大多数测试都是在Python中,没有真正的私有变量(成员,标识符,您想要调用它们)。一个人只是通过一个领先的下划线为用户建议隐私,并在测试代码时自由地忽略它。在静态类型的OO语言中,我想通过将私有成员转换为受保护的子类并将子类化为要测试的对象,可以使私有成员可以访问测试。在Javascript中,后者不适用,而前者似乎是不好的做法。

Most of the tests I've written previously have been in Python, where there are no real private variables (members, identifiers, whatever you want to call them). One simply suggests privacy via a leading underscore for the users, and freely ignores it while testing the code. In statically typed OO languages I suppose one could make private members accessible to tests by converting them to be protected and subclassing the object to be tested. In Javascript, the latter doesn't apply, while the former seems like bad practice.

我总是可以回到黑盒测试,只需检查最终结果。这是最简单,最干净的方法,但遗憾的是并不能满足我的需求。

I could always fall back to black box testing and simply check the final results. It's the simplest and cleanest approach, but unfortunately not really detailed enough for my needs.

那么,是否有一种标准方法可以将变量保密,同时保留一些变量在Javascript中进行测试的后门程序?

推荐答案

没有。我不相信有。它基本上归结为你是否采取封闭方法并放弃白盒测试或进行白盒测试并使用名称装饰为私人成员。实际上不仅在Python中,而且在javascript中有太多项目使用不那么神奇的下划线来装饰私有。所以在某种程度上,这已经是一个被广泛接受的问题解决方案。

No. I don't believe there is. It basically boils down to whether you take the closure approach and relinquish white box tests or do white box tests and use name decoration for "private" members. Actually not only in Python, but in javascript too many projects use the not so magic underscore to decorate privates. So in a way this is already a widely accepted solution to the problem.

如果你不想那样,真的,真的需要白盒测试,那么你始终可以将测试集成到您的对象中。如果您有生产代码的单独构建步骤(最小化,需要/提供分辨率等),那么您可以在此过程中删除测试函数。

If you don't want that and really, really need white-box unit testing, then you can always integrate the tests into your objects. If you have a separate build step for production code (minimization, require/provide-resolution, etc) then you can remove the test functions in this process.

这篇关于Javascript中的白盒测试 - 如何处理隐私?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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