【发布时间】:2016-11-22 00:21:33
【问题描述】:
我有一些异步函数需要通过 Intern 进行测试。每个都使用 ES6 承诺。
在 Intern 文档中,它说返回 promise 的测试函数会在该 promise 得到解决后立即通过。但是,我不仅关心我正在测试的异步函数是否被解析/拒绝,我还想检查以确保解析的值是正确的。
例子:我有我的功能:
function getSomething() {
return new Promise((resolve, reject) => {
// do some async stuff and resolve when done
setTimeout(function() {
resolve('the value');
}, 1000);
});
}
那我就有测试方法了:
tdd.test('getSomething', function() {
return new Promise((resolve, reject) => {
getSomething().then(function(value) {
// I want to resolve/reject the promise BASED ON the assertion -- if the assertion fails, I want to reject the promise; if it succeeds, I want to resolve it
assert.equal(value, 'the value', 'getSomething should return "the value".');
resolve();
});
});
}
我注意到这不起作用——如果断言在我的测试中失败,resolve() 永远不会被调用。
我如何能够解决/拒绝以断言为条件的承诺?我应该将断言包装在 try/catch 中吗?我没有在 Intern 文档上看到任何关于此的文档(大多数使用 this.async() 而不是 ES6 承诺)。
【问题讨论】:
标签: intern