【发布时间】:2016-04-01 10:39:42
【问题描述】:
我正在使用jasmine 2.0。
我正在尝试理解done() 函数。
使用以下基本的茉莉花代码:
describe('Jasmine 2.0 done()', function() {
beforeEach(function () {
console.log('install');
jasmine.clock().install();
});
afterEach(function () {
console.log('uninstall');
jasmine.clock().uninstall();
});
it('should wait 1ms then complete', function (done) {
setTimeout(function(){
console.log('async');
expect(true).toBe(true);
done();
}, 1)
});
});
我认为我看到的会发生:
-
beforeEach运行,安装时钟,记录“安装” - 测试运行,setTimeout 没有做任何事情
- 测试等待 5 秒(jasmine 失败前的默认超时时间)
- 测试失败,因为从未调用过
done()。 - 我知道这一点是因为我收到错误:
Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. -
afterEach然后运行,卸载时钟并记录“卸载”
我会期待
-
beforeEach运行,安装时钟并记录“安装” - 测试运行,等待一毫秒,运行日志,expect 和
done() -
afterEach运行,卸载时钟并记录“卸载” - 测试通过,没有错误
我认为这是因为文档说
在调用 done 之前,此规范不会完成。
所以我假设afterEach 会等到done() 被调用来执行。
我也尝试在afterEach 中添加done()
afterEach(function (done) {
console.log('uninstall');
jasmine.clock().uninstall();
done();
});
这没什么区别。
为什么这个测试不成功?
我对@987654337@ 函数有什么不了解的地方?
plunker 显示问题
【问题讨论】:
标签: javascript unit-testing asynchronous jasmine