如何在jquery插件中对私有方法进行单元测试? [英] how to unit-test private methods in jquery plugins?

查看:133
本文介绍了如何在jquery插件中对私有方法进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个新手JQuery问题但是:

Perhaps this is a bit of a novice JQuery question but:


  • 正确的jquery插件写在一个闭包内

  • 因此只能从外部访问定义插件接口的方法

  • 有时(或很多次)可能需要辅助方法,因为暴露为没有意义插件接口的一部分(例如,因为它们改变了内部状态)。

  • 如何进行单元测试?

  • proper jquery plugins are written inside a closure
  • thus only methods defining the plugin interface are accessible from the outside
  • sometimes (or many times) one may need helper methods that it doesn't make sense to expose as part of plugin interface (for example because they alter internal state).
  • how do those get unit-tested?

例如,查看 blockUI 插件,如何安装,删除方法,重置获得单元测试?

For example, looking at blockUI plugin, how can methods install, remove, reset get unit-tested?

要绘制并行,用Java我会:

To draw a parallel, in Java I would:


  1. 创建一个仅包含公共方法的BlockUI接口(按定义)

  2. 创建一个实现上述接口的BlockUIImpl类。这个类将包含install(),remove(),reset()方法,这些方法可以是公共的,或者(包)受保护的

所以,我会对Impl进行单元测试,但客户端程序员会通过BlockUI接口与插件进行交互。

So, I would unit-test the Impl but client programmers would interact with the plugin via BlockUI interface.

推荐答案

这同样适用于任何其他语言和测试私有方式:要测试私有方法,您应该通过公共方法来练习它们接口。换句话说,通过调用您的公共方法,私有方法在此过程中进行测试,因为公共方法依赖于私有。

The same applies here as with any other language and testing privates: To test private methods, you should exercise them via the public interface. In other words, by calling your public methods, the private methods get tested in the process because the public methods rely on the privates.

通常,私有方法不会单独测试公共接口 - 整个观点是它们是实现细节,测试通常不应该对实现的具体细节有太多了解。

Generally private methods are not tested separately from the public interface - the entire point is that they are implementation details, and tests should generally not know too much about the specifics of the implementation.

这篇关于如何在jquery插件中对私有方法进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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