在測試的時候瞬内,我們難免會使用測試替身幫助我們聚焦測試的關注點限书。測試替身主要有5種。
Dummy
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
dummy這個東西會被傳入測試方法中能真,但是不會被測試方法使用,基本都是作為測試方法的占位符舟陆,現在測試中基本不會出現耻矮。
Fake
Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
Fake是假冒偽劣產品,基本功能還是有的踱承,但是都是縮水版的哨免,不能用在生產環(huán)境。比如载荔,數據可能要從數據庫取采桃,而Fake的就是從本地文件中取。也不經常用到
Stubs
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
Stubs俗稱打樁工扎。它是一組定義好的request/response集合衔蹲。當request找不到的時候,response則為空橱健。
Spies
Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
Spies指的是間諜,它不會改變原有代碼的行為笨忌,但是會在運行測試的時候記錄一些你需要的信息俱病。
Mock
Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
Mock定義了不同的請求的expection是什么,只關注方法/行為途凫,不關注結果和實現的細節(jié)