Angular单元测试里pipe的mock设计
生活随笔
收集整理的這篇文章主要介紹了
Angular单元测试里pipe的mock设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我想對下列這個Component的UI進行單元測試。其中有一個自定義的cxUrl pipe,在單元測試時,我需要對其進行Mock:
解決方法,在單元測試的實現代碼里,創建一個用@Pipe修飾過的MockUrlPipe,在其transform方法里什么都不做即可:
運行時的截圖:
refreshView里執行視圖的template函數:
templateFn:
準備展開routerLink:
/*** Invokes a pipe with 1 arguments.** This instruction acts as a guard to {@link PipeTransform#transform} invoking* the pipe only when an input to the pipe changes.** @param index Pipe index where the pipe was stored on creation.* @param slotOffset the offset in the reserved slot space* @param v1 1st argument to {@link PipeTransform#transform}.** @codeGenApi*/只有當pipe的輸入參數發生變化時,??pipeBind1才會觸發:
pipeInstance.transform就是單元測試代碼里的pipe mock實現:
綁定表達式的值發生變化時,觸發pureFunction1Internal,重新計算綁定的值:
最終調用到我實現的Mock pipe:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
總結
以上是生活随笔為你收集整理的Angular单元测试里pipe的mock设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 安装Erlang
- 下一篇: SAP Spartacus 中,Angu