我已阅读有关 Promises not returning from handlers 的新警告并提出了相关问题...
在我的一些单元测试中,我使用 Jasmine 的 spy 功能来消除某些依赖项的功能以返回 promise 。因此,通过这一新的更改,我在测试运行时看到了很多警告。我当然可以禁用这些警告,但我想知道是否可以使用一种改进的方法来自然地消除此类错误?
示例:
beforeEach(function (done) {
var formatter = new Formatter();
var promise = Promise.resolve(['1,000', '2.45']);
spyOn(formatter, 'format').and.returnValue(promise);
// internally calls formatter.format()
doStuff(formatter, [1000, 2.4567]).then(done);
// promise is not returned
});
如果有用的话可以添加一个完整的工作插件吗?
最佳答案
问题在于 jasmine does not 的 done
回调返回任何内容,因此当您执行 .then(done)
时,您会触发 bluebird 的 a promise was created in a handler but none were returned from it 警告.
我通过向 Promise 对象添加一个新方法解决了这个问题:
var Promise = require('bluebird');
Promise.prototype.jasmineDone = function(done) {
return this.then(function() {
done();
return null;
}, function(err) {
done.fail(err);
return null;
});
};
然后我重构我的规范(这会触发警告):
it('should ...', function(done) {
doSomething().then(done, done.fail);
});
致:
it('should ...', function(done) {
doSomething().jasmineDone(done);
});
关于javascript - Jasmine 测试中 Bluebird promise 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35287107/