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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14315568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:17:09  来源:igfitidea点击:

How to use jasmine to test an async function that takes a long time to respond?

javascriptjasmine

提问by hbrls

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

我正在使用一个函数从 webapi 获取数据。基本上使用$.ajax.

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

我现在正在用waits()这样的方式测试它:

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();
    });
  });
});

The problem is:

问题是:

  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.
  1. 不能保证waits(2000)能很好地完成这项工作。由于各种原因(网络连接,api本身的算法效率等),我可能需要waits(5000)或更多,或者对于某些模型waits(500)就足够了。而最烦人的是,一切都失控了。
  2. 很多waits()使测试规范运行浪费大量时间等待。运行整个套件的时间太长,无法接受。

Is there some best practiceof doing there kind of things?

是否有一些best practice做那里的事情?

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.

PS:我知道单元测试不应该应用于某些依赖于 webapi 或数据库的功能。但是我正在使用单页js-heavy-webapp。数据获取过程与我将如何使用 js 模型使用它们一样重要。

回答by istepaniuk

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.

waitsFor()将等待指定的闩锁回调返回true(每隔几毫秒会尝试多次)。如果超过指定的超时(在本例中为 5000 毫秒),它也会引发异常。

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();
    });

  });
});

Check the Jasmine docsfor more info on waitsFor()and runs()

查看Jasmine 文档以获取更多信息waitsFor()runs()

回答by zbynour

The following solution allows you to wait no more than really necessary but still you have to define max timeout you suppose to be enough. The waitsFortakes the function and waits until it returns trueor the timeout passed as the last argument expired. Otherwise it fails.

以下解决方案允许您等待不超过真正必要的时间,但您仍然必须定义您认为足够的最大超时时间。该waitsFor直到它返回需要的功能和等待true或作为最后一个参数传递的超时。否则失败。

Supposing the thing you need to wait for is that r[0]is defined at all, it could be:

假设您需要等待的事情是r[0]完全定义的,它可能是:

waitsFor(
    function() { return r[0]; },
    'the data should be already set',
    5000);

回答by Amit Kumar Gupta

As per jasmine 2.5, you can pass an extra paramater for it("scenario", callback, timeout)

根据jasmine 2.5,您可以传递一个额外的参数it("scenario", callback, timeout)

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

  it('fetchFilter', function () {

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

    setTimeout(function() {
        done();
    }, 9000); 

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

  });
},10000);