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

如何使用 jasmine 測試一個需要很長時間才能響應

How to use jasmine to test an async function that takes a long time to respond?(如何使用 jasmine 測試一個需要很長時間才能響應的異步函數?)
本文介紹了如何使用 jasmine 測試一個需要很長時間才能響應的異步函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用一個函數從 webapi 獲取數據.基本使用$.ajax.

I'm using a function to fetch data from webapi. Basicly using $.ajax.

我現在用 waits() 測試它,如下所示:

I'm now testing it with waits() like this:

describe('xxxxxxxxxxxxxxxxxxxxx', function () {
  var r;
  it('fetchFilter', function () {
    runs(function () {
      model.fetch(opts)
      .done(function(data) {
        r = data;
      });
    });

    waits(2000);

    runs(function () {
      expect(r[0].gender).toBeDefined();
    });
  });
});

問題是:

  1. 不保證 waits(2000) 能很好地完成這項工作.由于各種原因(網絡連接、api本身的算法效率等),我可能不得不waits(5000)或更多,或者對于某些模型waits(500) 就足夠了.而最煩人的是,一切都失控了.
  2. 大量的 waits() 使得 test-specs-runs 浪費了大量的時間等待.運行整個套件的時間太長,無法接受.
  1. It's not guaranteed that waits(2000) will do the job well. Due to various reasons(network connections, algorithm efficiency of the api it self, etc.), I may have to waits(5000) or more, or for some models waits(500) is enough. And the most annoying thing is that it's all out of control.
  2. A lot of waits() makes the test-specs-runs waste a lot of time waiting. The time of running the whole suite is too long to accept.

是否有一些最佳實踐可以做這些事情?

Is there some best practice of doing there kind of things?

PS:我知道單元測試不應該應用于某些依賴 webapi 或數據庫的功能.但我正在使用單頁 js-heavy-webapp.數據獲取過程與我將如何在 js 模型中使用它們一樣重要.

PS: I know that unit test should not be applied to some function that relies on webapi or database. But I'm working with a single-page-js-heavy-webapp. The data fetching process is as important as how I will consume them with js models.

推薦答案

waitsFor() 會等待指定的latch回調返回true(會嘗試很多次每隔幾毫秒).如果超過指定的超時時間(本例中為 5000 毫秒),它也會引發異常.

waitsFor() will wait for a specified latch callback to return true (it will try many time every few ms). It will also raise an exception if the specified timeout (5000ms in this case) is exceeded.

describe('xxxxxxxxxxxxxxxxxxxxx', function () {
  var r, fetchDone;

  it('fetchFilter', function () {

    runs(function () {
      model.fetch(opts).done(function(data) {
        r = data;
        fetchDone = true;
      });
    });

    waitsFor(function() { 
      return fetchDone; 
    }, 5000); 

    runs(function () {
      expect(r[0].gender).toBeDefined();
    });

  });
});

查看 Jasmine 文檔 了解有關 waitsFor() 的更多信息和runs()

Check the Jasmine docs for more info on waitsFor() and runs()

這篇關于如何使用 jasmine 測試一個需要很長時間才能響應的異步函數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 免费看一区二区三区 | 成年无码av片在线 | 亚洲aⅴ一区二区 | 亚洲三区在线观看 | 欧美性网 | www.蜜桃av.com | 91免费版在线观看 | 亚洲欧美日韩在线 | 七七婷婷婷婷精品国产 | 天堂久久一区 | 久久国产精品-国产精品 | 丁香婷婷久久久综合精品国产 | 精品国模一区二区三区欧美 | 女朋友的闺蜜3韩国三级 | 一区中文| 国产一区二区三区网站 | www.黄色网 | 国产精品亚洲精品 | 亚洲国产日韩一区 | 日韩三级在线观看 | 国产偷自视频区视频 | 99国产精品一区二区三区 | 欧美精品二区 | 四虎永久免费影院 | 亚洲精品美女视频 | 乳色吐息在线观看 | 91高清免费 | 午夜视频在线免费观看 | 久久精品久久久久久 | 天天躁日日躁狠狠的躁天龙影院 | 羞羞的视频免费在线观看 | 国产精品96久久久久久 | 一级免费毛片 | 久久久久精 | 国产精品日韩在线观看一区二区 | 99精品国产一区二区三区 | 欧洲一级毛片 | 国产成人精品网站 | 免费久久久 | 毛片国产 | 久久99精品久久久 |