gpt4 book ai didi

mocha.js - Mocha 茶 Sequelize : I can't make tests fail

转载 作者:行者123 更新时间:2023-12-03 22:19:45 25 4
gpt4 key购买 nike

我正在尝试为 sequelize 中的模型编写测试,但我不明白为什么它没有失败

it('should find user by id', (done) => {
users.findByPk(2)
.then((retrievedUser) => {
expect(retrievedUser.dataValues).to.deep.equal('it should break');
done();
})
.catch((err) => {
console.log(`something went wrong [should find user by id] ${err}`);
done();
})
});

当我运行测试时,输出如下
something went wrong [should find user by id] AssertionError: expected { Object (id, email, ...) } to deeply equal 'it should break'
1 -__,------,
0 -__| /\_/\
0 -_~|_( ^ .^)
-_ "" ""

1 passing (40ms)

如果有人想看完整代码,我创建了一个 project

最佳答案

要使异步 Mocha 测试失败,请将错误作为参数传递给 done 回调函数

it('should find user by id', (done) => {
users.findByPk(2)
.then((retrievedUser) => {
expect(retrievedUser.dataValues).to.deep.equal('it should break');
done();
})
.catch((err) => {
console.log(`something went wrong [should find user by id] ${err}`);
done(err);
})
});

或者,使用没有回调的异步函数:
it('should find user by id', async () => {
const retrievedUser = await users.findByPk(2);
try {
expect(retrievedUser.dataValues).to.deep.equal('it should break');
} catch (err) {
console.log(`something went wrong [should find user by id] ${err}`);
throw err;
}
});

也就是说,我不建议记录失败测试的错误消息,因为这就是 Mocha 在典型设置中已经为您做的事情。所以我会去掉上面例子中的 try-catch 块。
it('should find user by id', async () => {
const retrievedUser = await users.findByPk(2);
expect(retrievedUser.dataValues).to.deep.equal('it should break');
});

关于mocha.js - Mocha 茶 Sequelize : I can't make tests fail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350065/

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