Angular单元测试里pipe的mock设计
我想對(duì)下列這個(gè)Component的UI進(jìn)行單元測(cè)試。其中有一個(gè)自定義的cxUrl pipe,在單元測(cè)試時(shí),我需要對(duì)其進(jìn)行Mock:
解決方法,在單元測(cè)試的實(shí)現(xiàn)代碼里,創(chuàng)建一個(gè)用@Pipe修飾過(guò)的MockUrlPipe,在其transform方法里什么都不做即可:
運(yùn)行時(shí)的截圖:
refreshView里執(zhí)行視圖的template函數(shù):
templateFn:
準(zhǔn)備展開(kāi)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*/只有當(dāng)pipe的輸入?yún)?shù)發(fā)生變化時(shí),??pipeBind1才會(huì)觸發(fā):
pipeInstance.transform就是單元測(cè)試代碼里的pipe mock實(shí)現(xiàn):
綁定表達(dá)式的值發(fā)生變化時(shí),觸發(fā)pureFunction1Internal,重新計(jì)算綁定的值:
最終調(diào)用到我實(shí)現(xiàn)的Mock pipe:
要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的Angular单元测试里pipe的mock设计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CentOS 安装Erlang
- 下一篇: SAP Spartacus 中,Angu