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

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

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)