摩卡异步测试句柄错误 [英] Mocha async test handle errors
问题描述
我正在尝试用Mocha创建一个测试用例,但是我的代码是异步的.
I'm trying to create a test case with Mocha but my code is asynchronous.
很好,我可以在"it"上添加一个完成"的回调函数,对于肯定的情况,它可以很好地工作.但是,当尝试测试否定案例时,只会使测试失败.
That's fine, I can add a "done" callback function to "it" and that will work perfectly fine for positive cases. But when trying to test negative cases, it will just make the test fail.
我想做这样的事情,但要异步:
I would like to make something like this but asynchronous:
someObject.someMethod(null).should.equal(false)
相反,我只能测试返回的回调,而不能测试实际发生的情况(null无效):
Instead I can only test for a callback to return, instead of testing what really happend (null is not valid):
it('this should return false or an error', function(done) {
someObject.someMethod(null, '', done);
});
我想写这样的东西:
it('this should return false or an error', function(done) {
someObject.someMethod(null, '', done).should.throw();
});
但这会导致此错误:
"TypeError: Cannot read property 'should' of undefined"
我也尝试使用Expect和assert,但是同样的规则适用.
I also tried using expect and assert, but the same rules apply.
有任何线索吗? 谢谢
编辑#1:
我尝试失败:
使用true/false
Using true/false
return (true); // ok
retur (false); // error
使用回调:
callback(); // ok
callback(err); // error
使用callback2:
Using callback2:
callback(true); // ok
callback(false); // error
使用callback3:
Using callback3:
callback(null); // ok
callback(new Error()); // error
使用抛出(也在try/catch块中):
Using throw (also inside a try/catch block):
// a
throw new Error();
callback();
// b
throw new Error();
callback(new Error());
// c
throw new Error();
callback(false);
我还尝试了几种组合,例如返回和调用回调,成功返回但在错误时抛出或调用回调,反之亦然.
I have also tried several combinations such as returning and calling a callback, returning on success but throwing or calling a callback on error and conversely..
编辑#2:
it('should throw', function(done) {
(function(done) {
someObject.someMethod(null, '', done)
}).should.throw();
});`
编辑#3:
要总结在测试方面:
it('this should return false or an error', function(done) {
someObject.someMethod(null, function(err, value) {
expect(value === false || err instanceof Error).toEqual(true);
return done();
});
});
在代码端:function(var,callback)
And on the code-side: function(var, callback)
...
callback(new Error('your comments'), false);
// or
callback(new Error('your comments'));
...
// if successful:
callback();
// or
callback(null);
// or
callback(null, 'foo');
推荐答案
假定someObject.someMethod
遵守常规的Node.js回调约定(错误作为第一个参数,值作为第二个参数),则可以使用以下内容: /p>
Assuming that someObject.someMethod
adheres to the regular Node.js callback convention (error as first argument, value as second), you could use something like this:
it('this should return false or an error', function(done) {
someObject.someMethod(null, '', function(err, value) {
(err instanceof Error || value === false).should.equal(true);
return done();
});
});
我从测试用例的名称推断出要在两种情况下进行测试:方法是返回"错误,或者是返回"值false
(通过使用适当的参数).
I inferred from the name of the test case that you want to test for two situations: either the method is "returning" an error, or it's "returning" a value of false
(by calling the callback function with the appropriate arguments).
这篇关于摩卡异步测试句柄错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!