Mocha/Chai测试返回错误消息,我找不到测试它的方法



标题说明了一切,它返回这样的消息错误:startDate是必填字段我尝试使用equal, instanceof.

describe('filter', () => {
it('needs to return a startDate required message', async () => {
let dto = {
'endDate': '2000-02-02',
};
let result = await service.filter(dto);
expect(result).to.throw();
};
});

这里的问题是您没有测试错误。

想想看:当你在做expect(result).to.throw();时,错误已经被抛出。

并且result没有抛出任何错误。

所以你可以测试调用函数时抛出的错误。

你可以用chai来做,就像承诺的那样:

service.filter(dto).should.be.rejected;

另外,我已经用下面的代码测试了你的方法:

describe('Test', () => {
it('Test1', async () => {
//Only this line pass the test
thisFunctionThrowAnError().should.be.rejected;
//This not pass
let result = await thisFunctionThrowAnError();
expect(result).to.throw();
});
});
async function thisFunctionThrowAnError(){
throw new Error("Can mocha get this error?")
}

最新更新