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

2022-01-11 00:00:00 jasmine javascript

我正在使用一个函数从 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()

相关文章