javascript - Jasmine 可以用来测试并发失败吗?

标签 javascript jasmine settimeout

根据其他响应(如 Jasmine: test a setTimeout function throws an errorHow to test a function which has a setTimeout with jasmine? ),我怀疑这里没有好的解决方案,但我想弄清楚如何更好地处理下面的第二个测试:

describe('Tests', function () {
    it('succeed', function () { expect(true).toBeTruthy(); });
    it('have problems', function(done) {
        setTimeout(function() { console.log(undefined.foo); });
        setTimeout(done, 5);
    });

    it('fail', function() { fail; });
    it('also fail', function() { fail; });
});

Jasmine 当前的行为是运行第一个测试,然后在第二个测试中遇到导致 setTimeout 的流氓异常时退出;最后两个失败的规范从未运行。

当然,我的用例看起来不是这样的!异步错误发生在调用堆栈下方、河流上方和树林中的某个地方。这显然是一个错误,它发生了却没有被捕获!但不幸的是,如果此类错误总是会终止 Jasmine 本身,而不是导致测试用例失败。

最佳答案

我相信您想要 try...catch 或 Promise 以及 Jasmine 的 done.fail()

promise :

it('gets some huge file', done => {
  getFile('http://movies.com/holymountain.mp4')
    .then(res => {
      expect(res.status).toBe(200)
      done()
    })
    .catch(done.fail)
})

尝试/捕捉

it('gets some huge file', done => {
  try {
    getFile('http://movies.com/holymountain.mp4')
    expect(getFile.status).toBe(200)
    done()
  } catch (e) {
    // console.log(e) // ???
    done.fail()
  }
})

Issue for reference

关于javascript - Jasmine 可以用来测试并发失败吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40389940/

相关文章:

javascript - 简单的 javascript 'new' 关键字

php - 管理多个 Cookie "Domains"

javascript - 具有嵌套 forEach 和 for 循环的函数不会返回 false

javascript - 该单元测试失败的原因是什么?

javascript - Protractor :如何检查中继器中是否完全出现一个条目

jquery - 间隔触发点击

jquery - 使用when和done和settimeout显示隐藏div

javascript - 如何在 jquery .post 中将二进制文件保存在客户端上

javascript - 异步方法中的 Protractor 计数表行

node.js - 如何在异步等待调用 Node 上设置超时