【发布时间】:2019-02-24 18:06:59
【问题描述】:
第一次真正使用 sinon,我在使用模拟库时遇到了一些问题。
我要做的只是从名为myMethod 的dao 类中存根/模拟一个函数。不幸的是,我收到了错误:myMethod is not a function,这让我相信我要么将 await/async 关键字放在测试的错误位置,要么我不理解 sinon 100% 的存根。代码如下:
// index.js
async function doWork(sqlDao, task, from, to) {
...
results = await sqlDao.myMethod(from, to);
...
}
module.exports = {
_doWork: doWork,
TASK_NAME: TASK_NAME
};
// index.test.js
const chai = require("chai");
const expect = chai.expect;
const sinon = require("sinon");
const { _doWork, TASK_NAME } = require("./index.js");
const SqlDao = require("./sqlDao.js");
.
.
.
it("given access_request task then return valid results", async () => {
const sqlDao = new SqlDao(1, 2, 3, 4);
const stub = sinon
.stub(sqlDao, "myMethod")
.withArgs(sinon.match.any, sinon.match.any)
.resolves([{ x: 1 }, { x: 2 }]);
const result = await _doWork(stub, TASK_NAME, new Date(), new Date());
console.log(result);
});
有错误:
1) doWork
given task_name task then return valid results:
TypeError: sqlDao.myMethod is not a function
【问题讨论】:
标签: javascript node.js async-await sinon sinon-chai