【发布时间】:2014-12-31 07:32:03
【问题描述】:
由于 start()、stop() 将在 Qunit 2.0 中被删除,通过 beforeEach、afterEach 方法进行异步设置和拆卸的替代方案是什么?例如,如果我希望 beforeEach 等待承诺完成?
【问题讨论】:
标签: testing asynchronous qunit
由于 start()、stop() 将在 Qunit 2.0 中被删除,通过 beforeEach、afterEach 方法进行异步设置和拆卸的替代方案是什么?例如,如果我希望 beforeEach 等待承诺完成?
【问题讨论】:
标签: testing asynchronous qunit
QUnit 基本上希望人们停止使用global methods(不仅仅是start() 和stop(),还有test()、expect() 等)。因此,从版本 1.16.0 开始,您应该始终使用全局命名空间 (QUnit) 或传递给 test() 函数的 assert API 参数。这包括新的async control:
QUnit.test( "testing async action", function( assert ) { // <-- note the `assert` argument here
var done = assert.async(); // tell QUnit we're doing async actions and
// hold onto the function it returns for later
setTimeout(function() { // do some async stuff
assert.ok( true, "This happened 100 ms later!" );
done(); // using the function returned from `assert.async()` we
// tell QUnit we're don with async actions
}, 100);
});
如果您熟悉旧的 start() 和 stop() 的做事方式,您应该会发现这非常相似,但更加分隔和可扩展。
因为async()方法调用是在assert参数进入测试,所以不能在beforeEach()函数中使用。如果你有一个例子说明你之前是如何做到的,请发布它,我们可以尝试找出如何将它融入新的方式。
更新
我之前的错误是,assert 对象被传递到模块上的 beforeEach 和 afterEach 回调中,因此您应该能够执行与测试相同的逻辑:
QUnit.module('set of tests', {
beforeEach: function(assert) {
var done = assert.async();
doSomethingAsync(function() {
done(); // tell QUnit you're good to go.
});
}
});
(在 QUnit 1.17.1 中测试)
【讨论】:
QUnit.config.autostart = false; 延迟QUnit 的启动,然后在$.get() 完成保存返回数据并调用QUnit.start();让事情顺利进行。
QUnit.start() 只能被调用一次来启动整个测试套件。对吗?
assert 对象正在被传递到 beforeEach/afterEach 方法中!我会更新我的答案,但看起来你可以使用与模块设置和拆卸测试中相同的语法。
看到没有人回答 beforeEach/afterEach 部分:测试套件应该在页面加载后立即运行。如果不能立即实现,则求助于配置 QUnit:
QUnit.config.autostart = false;
并继续设置您的测试套件(初始化测试,将它们提供给 QUnit,异步等待某些组件加载,无论是 AJAX 还是其他任何东西),您的站点,最后,当它准备好时,运行:
QUnit.start();
QUnit's docsite 已覆盖。
【讨论】:
Ember Qunit,曾经存在过beforeEach/setup,afterEach/teardown并存了一段时间。
【讨论】: