node.js - 用 sinon 进行单元测试
问题描述
我正在尝试模拟以下代码:
model.findAndCountAll({
pos: start
}).then((data) => {
this.success(....);
}).error(error => {
this.error(error, this.constants.HTTP_CODE_INTERNAL_SERVER_ERROR)
})
下面是模拟上述代码的代码:
const findAndCountAll = sinon.stub(model, 'findAndCountAll');
findAndCountAll.withArgs().returns(new Promise((resolve, reject) => {
resolve(data);
}));
如果我从要模拟的代码中删除错误部分,它会起作用,否则会出现以下错误:
TypeError: model.findAndCountAll(...).then(...).error is not a function
解决方案
推荐阅读
- android - 从活动中获取嵌套在导航控制器内的片段实例
- javascript - 光滑的轮播不工作,我做错了什么?
- asp.net-core - 拒绝不具有预期结构的 HTTP 请求
- uiscrollview - SwiftUI ScrollView 奇怪的垂直拉伸
- java - 如何检查数组是否已满,如果未满则添加?
- python - 如何在同一个脚本 PYTHON 上绘制多个图形
- vector - SystemVerilog 如何执行 one-hot masking
- influxdb - 在 InfluxDB 中使用不同操作对事件计数进行建模
- ruby - 如何使用 Go 共享库在 Ruby 中传递字符串数组并获取字符串数组?
- c - VSCode包括c中的头文件