日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Angular单元测试里pipe的mock设计

發(fā)布時(shí)間:2023/12/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Angular单元测试里pipe的mock设计 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我想對(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):


/*** If the value of the provided exp has changed, calls the pure function to return* an updated value. Or if the value has not changed, returns cached value.** @param lView LView in which the function is being executed.* @param bindingRoot Binding root index.* @param slotOffset the offset from binding root to the reserved slot* @param pureFn Function that returns an updated value* @param exp Updated expression value* @param thisArg Optional calling context of pureFn* @returns Updated or cached value*/

綁定表達(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。