問題描述
也許這是一個(gè) JQuery 新手問題,但是:
Perhaps this is a bit of a novice JQuery question but:
- 適當(dāng)?shù)?jquery 插件寫在閉包內(nèi)
- 因此只有定義插件接口的方法可以從外部訪問
- 有時(shí)(或多次)可能需要輔助方法,將其作為插件接口的一部分公開是沒有意義的(例如,因?yàn)樗鼈儠?huì)改變內(nèi)部狀態(tài)).
- 如何進(jìn)行單元測(cè)試?
比如看blockUI插件,怎么能方法安裝、刪除、重置是否經(jīng)過單元測(cè)試?
For example, looking at blockUI plugin, how can methods install, remove, reset get unit-tested?
為了畫一個(gè)平行線,在 Java 中我會(huì):
To draw a parallel, in Java I would:
- 創(chuàng)建一個(gè)僅包含公共方法的 BlockUI 接口(根據(jù)定義)
- 創(chuàng)建一個(gè)實(shí)現(xiàn)上述接口的 BlockUIImpl 類.此類將包含可能是公共的或(包)受保護(hù)的 install()、remove()、reset() 方法
因此,我將對(duì) Impl 進(jìn)行單元測(cè)試,但客戶端程序員將通過 BlockUI 接口與插件進(jìn)行交互.
So, I would unit-test the Impl but client programmers would interact with the plugin via BlockUI interface.
推薦答案
這同樣適用于任何其他語言和測(cè)試私有方法:要測(cè)試私有方法,您應(yīng)該通過公共接口來使用它們.換句話說,通過調(diào)用您的公共方法,私有方法在進(jìn)程中得到測(cè)試,因?yàn)楣卜椒ㄒ蕾囉谒接蟹椒?
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.
通常私有方法不會(huì)與公共接口分開測(cè)試 - 重點(diǎn)在于它們是實(shí)現(xiàn)細(xì)節(jié),測(cè)試通常不應(yīng)該對(duì)實(shí)現(xiàn)的細(xì)節(jié)了解太多.
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.
這篇關(guān)于如何對(duì) jquery 插件中的私有方法進(jìn)行單元測(cè)試?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!