unit-testing - 对 Sequelize Model 的实例进行单元测试
问题描述
我有以下代码:
async save(id: string) {
const person = await PersonModel.findOne({
where: { id: id },
});
if (!person) {
await PersonModel.create({
id: '2345',
name: 'John Doe',
age: 25
});
return;
}
await person.increment({ age: 15 });
}
现在,我想测试 person.increment() ,其中年龄将添加 15。我有以下代码来转义为模型创建新记录的条件。
const findOneFake = sinon.spy(() => {
return {}; //returns empty object or true
});
const proxy = (proxyquire('./path/to/file.ts', {
'./path/to/PersonModel.ts': {
default: {
findOne: findOneFake
}
}
})).default;
beforeEach(async () => {
await save();
});
it('should increment age with 15');
我要怎么做?我该怎么做才能测试它?我可以将 sinon.fake() 用于 PersonModel.create 或 PersonModel.update,但我很难测试 Sequelize 模型的实例。
解决方案
推荐阅读
- arcore - ARCore 增强图像跟踪质量
- python - ModuleNotFoundError:没有名为“tensorflow.keras.layers.pooling”的模块
- awk - 替换子字符串中的引号(最好没有外部依赖)
- ajax - Ajax:单击“无法设置属性‘值’为空”时更新文本字段值
- wordpress - Wordpress Gatsby.js 在 graphql staticQuery 中获取 databaseId
- c - 根据另一个字符串标记一个字符串
- arrays - 给定一个未排序的数组 A,在一个步骤中,您可以选择两个索引 i、j 并将 A[i] 替换为 A[i ]xor A[j]
- javascript - 当我单击切换按钮打开模式时,关闭并重新打开模式时,额外的单击事件再次触发
- excel - VBA 将粘贴的行设置为范围
- mysql - 如何对货币汇率表进行正确的 SQL 查询?