gpt4 book ai didi

node.js - 异步 Mocha 测试(使用 Chai 断言库)应该失败,但被标记为通过

转载 作者:搜寻专家 更新时间:2023-10-30 21:20:01 24 4
gpt4 key购买 nike

我使用 mocha 测试框架和 chai 断言库在 TypeScript 中编写了一些测试(我对这 3 个都是新手),发现抛出了错误并在下面的代码中调用了 assert.fail()。但是,测试仍标记为通过。我很困惑为什么会发生这种情况,以及我是否在 promise 方面做错了什么。此外,如下所示,还有一个 UnhandledPromiseRejectionWarning。我不明白为什么这被标记为未处理,因为我包含了一个 catch block 。我将不胜感激有关如何解决此问题的任何建议,谢谢!

   User.all()
.then((users) => {
expect(users.length).to.equal(0);
return User.create('test@test.email', '12345', 'test', true, true);
})
.then(() => {
return User.all();
})
.then((users) => {
expect(users.length).to.equal(1);
})
.catch((error) => {
assert.fail();
});

调试控制台

 ✓ should create a new record
with existing e-mail in the database
(node:29903) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): AssertionError: assert.fail()
(node:29903) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Code

Debug Console

最佳答案

正如 Benjamin Gruenbaum 在上面所说的,这是通过确保 it block 返回一个 promise 来解决的

it('should create a new record', () => {

return User.all()
.then((users) => {
expect(users.length).to.equal(0);
return User.create('test@test.email', '12345', 'test', true, true);
})
.then(() => {
return User.all();
})
.then((users) => {
expect(users.length).to.equal(1);
})
.catch((error) => {
assert.fail();
});
});

关于node.js - 异步 Mocha 测试(使用 Chai 断言库)应该失败,但被标记为通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42493113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com