【问题标题】:Can Jasmine be made to test for concurrency failures?可以让 Jasmine 测试并发故障吗?
【发布时间】:2016-11-02 21:16:11
【问题描述】:

我根据其他回复(如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 本身,而不是导致测试用例失败。

【问题讨论】:

    标签: javascript jasmine settimeout


    【解决方案1】:

    我相信你想要 try...catch 或与 Jasmine 的 done.fail() 一起承诺。

    承诺:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多