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

使用 $timeout 和 Jasmine 的模擬時鐘的單元測試 An

Unit Testing Angular Service that uses $timeout with Jasmine#39;s Mock Clock(使用 $timeout 和 Jasmine 的模擬時鐘的單元測試 Angular 服務)
本文介紹了使用 $timeout 和 Jasmine 的模擬時鐘的單元測試 Angular 服務的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我的一個 Angular 服務中有一個函數,我希望定期重復調用該函數.我想使用 $timeout 來做到這一點.它看起來像這樣:

I have a function inside one of my angular services that I'd like to be called repeatedly at a regular interval. I'd like to do this using $timeout. It looks something like this:

var interval = 1000; // Or something

var _tick = function () {
     $timeout(function () {
        doStuff();
        _tick();
    }, interval);
};

_tick();

目前,我對如何使用 Jasmine 進行單元測試感到困惑 - 我該怎么做?如果我使用 $timeout.flush() 那么函數調用會無限期地發生.如果我使用 Jasmine 的模擬時鐘,$timeout 似乎不受影響.基本上,如果我能做到這一點,我應該很高興:

I'm stumped on how to unit test this with Jasmine at the moment - How do I do this? If I use $timeout.flush() then the function calls occur indefinitely. If I use Jasmine's mock clock, $timeout seems to be unaffected. Basically if I can get this working, I should be good to go:

describe("ANGULAR Manually ticking the Jasmine Mock Clock", function() {
    var timerCallback, $timeout;

    beforeEach(inject(function($injector) {
        $timeout = $injector.get('$timeout');
        timerCallback = jasmine.createSpy('timerCallback');
        jasmine.Clock.useMock();
    }));

    it("causes a timeout to be called synchronously", function() {
        $timeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        jasmine.Clock.tick(101);
        expect(timerCallback).toHaveBeenCalled();
    });
});

這兩種變體有效,但對我沒有幫助:

These two variations work, but do not help me:

describe("Manually ticking the Jasmine Mock Clock", function() {
    var timerCallback;

    beforeEach(function() {
        timerCallback = jasmine.createSpy('timerCallback');
        jasmine.Clock.useMock();
    });

    it("causes a timeout to be called synchronously", function() {
        setTimeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        jasmine.Clock.tick(101);
        expect(timerCallback).toHaveBeenCalled();
    });
});

describe("ANGULAR Manually flushing $timeout", function() {
    var timerCallback, $timeout;

    beforeEach(inject(function($injector) {
        $timeout = $injector.get('$timeout');
        timerCallback = jasmine.createSpy('timerCallback');
    }));

    it("causes a timeout to be called synchronously", function() {
        $timeout(function() {
            timerCallback();
        }, 100);
        expect(timerCallback).not.toHaveBeenCalled();
        $timeout.flush();
        expect(timerCallback).toHaveBeenCalled();
    });
});

提前致謝!

推薦答案

不要使用 Jasmine 的時鐘使您的測試異步.相反,使用 $timeout.flush() 來同步維護測試流程.設置起來可能有點棘手,但一旦你得到它,你的測試就會更快,更可控.

Do not make your test Async by using Jasmine's clock. Instead, use $timeout.flush() to synchronously maintain the flow of the test. It may be a bit tricky to setup, but once you get it then your tests will be faster and more controlled.

下面是一個使用這種方法進行測試的示例:https://github.com/angular/angular.js/blob/master/test/ngAnimate/animateSpec.js#L618

Here's an example of a test that does it using this approach: https://github.com/angular/angular.js/blob/master/test/ngAnimate/animateSpec.js#L618

這篇關于使用 $timeout 和 Jasmine 的模擬時鐘的單元測試 Angular 服務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
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 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 久久久久久综合 | 91传媒在线观看 | 男女羞羞视频大全 | 亚洲欧美成人影院 | 欧美一区二区三区久久精品视 | 69福利影院 | 国产精品视频入口 | 成人av高清在线观看 | 国产在线二区 | 天天射网站 | 亚洲国产小视频 | 精品一区二区三区91 | 99r在线 | 国产精品福利视频 | 亚洲网站在线观看 | 国产精品a久久久久 | 欧美在线观看免费观看视频 | 久草视频在线播放 | 久久精品国产亚洲一区二区三区 | 色播久久 | 日韩欧美在线不卡 | 蜜桃一区二区三区在线 | 色综合成人网 | 国产黑丝av | 雨宫琴音一区二区在线 | 国产欧美精品一区 | 欧美福利一区 | 999久久久免费精品国产 | 91精品久久 | 神马九九 | 欧美无乱码久久久免费午夜一区 | 国产99久久精品 | 欧美小视频在线观看 | 亚洲精品一区中文字幕乱码 | 日韩一区二区在线视频 | 91精品国产自产在线老师啪 | 亚洲福利一区二区 | 欧美日韩国产精品一区 | 成人无遮挡毛片免费看 | 欧美日韩国产一区二区三区 | 久久综合狠狠综合久久综合88 |