久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

茉莉花鐘的工作原理是什么?

How jasmine clock works?(茉莉花鐘的工作原理是什么?)
本文介紹了茉莉花鐘的工作原理是什么?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我不想花幾個(gè)小時(shí)閱讀代碼來找到相關(guān)部分,但我很好奇 jasmine 如何實(shí)現(xiàn)它的時(shí)鐘.它的有趣之處在于它可以使用同步測試代碼來測試異步代碼.AFAIK,使用當(dāng)前支持 ES5 的 node.js,這是不可能的(異步函數(shù)在 ES7 中定義).它是否使用 estraverse 之類的東西解析 js 代碼并從同步代碼構(gòu)建異步測試?

I don't want to read code for hours to find the relevant part, but I am curious how jasmine implements its clock. The interesting thing with it is that it can test async code with sync testing code. AFAIK, with the current node.js, which supports ES5, this is not possible (async functions are defined in ES7). Does it parse the js code with something like estraverse and build an async test from the sync one?

只是我所說的一個(gè)例子:

Just an example of what I am talking about:

it("can test async code with sync testing code", function () {
    jasmine.clock().install();

    var i = 0;
    var asyncIncrease = function () {
        setTimeout(function () {
            ++i;
        }, 1);
    };

    expect(i).toBe(0);
    asyncIncrease();
    expect(i).toBe(0);
    jasmine.clock().tick(2);
    expect(i).toBe(1);

    jasmine.clock().uninstall();
});

這里 expect(i).toBe(1); 應(yīng)該在回調(diào)中.

In here the expect(i).toBe(1); should be in a callback.

推薦答案

install() 函數(shù)實(shí)際上將 setTimeout 替換為 jasmine 讓您可以更好地控制的模擬函數(shù).這使它同步,因?yàn)闆]有完成實(shí)際的等待.相反,您可以使用 tick() 函數(shù)手動(dòng)將其向前移動(dòng),該函數(shù)也是同步的.

The install() function actually replaces setTimeout with a mock function that jasmine gives you more control over. This makes it synchronous, because no actual waiting is done. Instead, you manually move it forward with the tick() function, which is also synchronous.

查看源代碼:https://github.com/jasmine/jasmine/blob/ce9600a3f63f68fb75447eb10d62fe07da83d04d/src/core/Clock.js#L21

假設(shè)您有一個(gè)內(nèi)部設(shè)置超時(shí) 5 小時(shí)的函數(shù).Jasmine 只是替換了該 setTimeout 調(diào)用,以便在您調(diào)用 tick() 時(shí)調(diào)用回調(diào),以便內(nèi)部計(jì)數(shù)器達(dá)到或超過 5 小時(shí)標(biāo)記.很簡單!

Suppose you had a function that internally set a timeout of 5 hours. Jasmine just replaces that setTimeout call so that the callback will be called when you call tick() so that the internal counter reaches or exceeds the 5 hour mark. It's quite simple!

這篇關(guān)于茉莉花鐘的工作原理是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認(rèn)為文檔“準(zhǔn)備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術(shù)
What do jasmine runs and waitsFor actually do?(jasmine 運(yùn)行和等待實(shí)際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對(duì)鏈?zhǔn)椒椒ㄟM(jìn)行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監(jiān)視函數(shù)中的函數(shù)調(diào)用?)
主站蜘蛛池模板: 亚洲精品久久久久久久不卡四虎 | 亚洲一二三区在线观看 | 夜夜草视频 | 免费观看日韩av | 国产精品成人一区二区三区 | 一二三区av | 国产区久久 | 国产一级在线 | 国产日韩欧美精品 | 国产精品特级片 | 天堂一区| 中文字幕 在线观看 | 日韩欧美国产一区二区三区 | 国产精品99久久久久久动医院 | 亚洲成人黄色 | 一二区成人影院电影网 | 国产一区二区精品在线 | 欧美精品一区二区在线观看 | 精品婷婷 | 久久久精品综合 | 二区高清| caoporn地址| 超碰免费在 | 欧美一级久久 | 国产一区二区三区在线 | 看a级黄色毛片 | 午夜a v电影| 国产农村一级国产农村 | 亚洲精品在线看 | 日韩精品一二三 | 久久99国产精一区二区三区 | www.一区二区三区 | 中国三级黄色录像 | 亚洲精品久久久久久久久久久久久 | 中文精品视频 | av网站免费观看 | 粉嫩国产精品一区二区在线观看 | 最新伦理片 | 成人免费影院 | 久久蜜桃av一区二区天堂 | av天天干|