如何在摩卡中处理异步测试?
How to handle async test in Mocha?
我有一个事件处理函数,它接受一些事件数据和一个回调函数作为输入。
此事件处理程序正在使用承诺来完成其工作:
function myHandler(event, callback) {
somePromise(event).then((result) => {
if (result.Error) {
callback(error);
} else {
callback(null, result.SuccessData);
}
});
}
我有以下代码来测试处理程序:
it('test handler', function(done) {
let event = {...};
myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
done();
}
});
当运行这个测试时,我收到这个错误:
(node:3508) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: expected 'unexpected' to equal 'expected'
所有测试结束:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
但测试仍在通过...
为什么在调用 done()
函数时出现此错误?
您正在使用 Promises。
您可以 return 您的 Promise 而不使用 done
,像这样:
// Note the absence of the done callback here
it('test handler', function() {
let event = {...};
return myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
}
});
或使用Chai As Promised:
it('test handler', function(done) {
let event = {...};
myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
}.should.notify(done)
});
我觉得后者更好,就好像你忘记了第一个例子中的 return
,你的测试可能会悄无声息地失败。
将您的测试包装到一个承诺中,如果断言失败则拒绝。
it('test handler', () => {
let event = {...}
return new Promise((resolve, reject) => {
myHandler(event, (error, success) => {
try {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
resolve();
} catch (err) {
reject(err);
}
});
});
});
我有一个事件处理函数,它接受一些事件数据和一个回调函数作为输入。
此事件处理程序正在使用承诺来完成其工作:
function myHandler(event, callback) {
somePromise(event).then((result) => {
if (result.Error) {
callback(error);
} else {
callback(null, result.SuccessData);
}
});
}
我有以下代码来测试处理程序:
it('test handler', function(done) {
let event = {...};
myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
done();
}
});
当运行这个测试时,我收到这个错误:
(node:3508) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: expected 'unexpected' to equal 'expected'
所有测试结束:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
但测试仍在通过...
为什么在调用 done()
函数时出现此错误?
您正在使用 Promises。
您可以 return 您的 Promise 而不使用 done
,像这样:
// Note the absence of the done callback here
it('test handler', function() {
let event = {...};
return myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
}
});
或使用Chai As Promised:
it('test handler', function(done) {
let event = {...};
myHandler(event, function(error, success) {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
}.should.notify(done)
});
我觉得后者更好,就好像你忘记了第一个例子中的 return
,你的测试可能会悄无声息地失败。
将您的测试包装到一个承诺中,如果断言失败则拒绝。
it('test handler', () => {
let event = {...}
return new Promise((resolve, reject) => {
myHandler(event, (error, success) => {
try {
expect(success).to.not.be.null;
expect(error).to.be.null;
expect(success.member).to.be.equal('expected');
resolve();
} catch (err) {
reject(err);
}
});
});
});