Node.js 当出现任何错误时,如何在承诺内做出断言';不要冒泡?
使用mocha运行此操作会导致超时,而不是让mocha捕获错误,从而使其立即失败Node.js 当出现任何错误时,如何在承诺内做出断言';不要冒泡?,node.js,promise,mocha.js,throw,Node.js,Promise,Mocha.js,Throw,使用mocha运行此操作会导致超时,而不是让mocha捕获错误,从而使其立即失败 var when = require('when'); var should = require('should'); describe('', function() { it('', function(done) { var d = when.defer(); d.resolve(); d.promise.then(function() {
var when = require('when');
var should = require('should');
describe('', function() {
it('', function(done) {
var d = when.defer();
d.resolve();
d.promise.then(function() {
true.should.be.false;
false.should.be.true;
throw new Error('Promise');
done();
}); }); });
有没有其他方法可以在承诺中做出断言,比如当他们失败时,他们会被摩卡抓住,导致它立即失败
根据chai的建议,我查看了它,似乎我必须直接访问promise对象,对吗?问题是我没有直接使用诺言。。如果我简化的话,我的缺点是,但这将是一个更接近现实的例子
function core_library_function(callback){
do_something_async(function which_returns_a(promise){
promise.then(function(){
callback(thing);
}); }); }
describe('', function() {
it('', function(done) {
core_library_function(function(thing){
...
done();
}); }); });
因此,我确实无法直接控制承诺,它被抽象得很远。当使用Mocha的承诺时,您必须在测试中返回承诺,并且由于没有使用回调,您需要删除
done
参数
it('', function() {
var d = when.defer();
d.resolve();
return d.promise.then(function() {
throw new Error('Promise');
});
});
这在以下文档中进行了描述:
或者,您可以返回承诺,而不是使用done()
回调
考虑CHAI作为约定,也发现在结束时调用的<代码> CcChar()/Case>函数工作得很好,调用<代码> DONE()/代码>内部并传递错误: