Javascript 可以让Jasmine测试并发失败吗?
根据其他回答(如和),我怀疑这里没有好的解决方案,但我想找出如何更好地处理下面的第二个测试:Javascript 可以让Jasmine测试并发失败吗?,javascript,jasmine,settimeout,Javascript,Jasmine,Settimeout,根据其他回答(如和),我怀疑这里没有好的解决方案,但我想找出如何更好地处理下面的第二个测试: describe('Tests', function () { it('succeed', function () { expect(true).toBeTruthy(); }); it('have problems', function(done) { setTimeout(function() { console.log(undefined.foo); });
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本身,而不是导致测试用例失败。我相信您希望
尝试…捕获或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()
}
})
我相信你希望尝试一下……抓住或承诺,与Jasmine的一起完成。失败()
承诺:
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()
}
})