問題描述
也許這是一個 JQuery 新手問題,但是:
Perhaps this is a bit of a novice JQuery question but:
- 適當的 jquery 插件寫在閉包內
- 因此只有定義插件接口的方法可以從外部訪問
- 有時(或多次)可能需要輔助方法,將其作為插件接口的一部分公開是沒有意義的(例如,因為它們會改變內部狀態).
- 如何進行單元測試?
比如看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:
- 創建一個僅包含公共方法的 BlockUI 接口(根據定義)
- 創建一個實現上述接口的 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 插件中的私有方法進行單元測試?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!