如何使用 jasmine 测试一个需要很长时间才能响应的异步函数?

How to use jasmine to test an async function that takes a long time to respond?(如何使用 jasmine 测试一个需要很长时间才能响应的异步函数?)
本文介绍了如何使用 jasmine 测试一个需要很长时间才能响应的异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用一个函数从 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 测试一个需要很长时间才能响应的异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Drag amp; Drop with Protractor by Repeater(拖动amp;通过中继器使用量角器掉落)
Getting the position of the element in a list when it#39;s drag/dropped (ui.sortable)(拖放时获取元素在列表中的位置(ui.sortable))
Detecting HTML5 Drag And Drop support in javascript(在 javascript 中检测 HTML5 拖放支持)
HTML5 drop event doesn#39;t work unless dragover is handled(除非处理了拖动,否则 HTML5 放置事件不起作用)
How to use jQuery#39;s drop event to upload files dragged from the desktop?(如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?)
Drop image into contenteditable in Chrome to the cursor(将图像拖放到 Chrome 中的 contenteditable 到光标处)