使用javascript与Mocha和Sinon进行单元测试问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用javascript与Mocha和Sinon进行单元测试问题相关的知识,希望对你有一定的参考价值。
我正在尝试使用mocha和sinon来测试使用AWS服务的一段代码。代码下方:
exports.init = ({ athenaClient }) => {
const command = {};
command.execute = sqlCommand => {
const params = {
QueryString: sqlCommand,
QueryExecutionContext: {
Database: process.env.ATHENA_DB || "default"
}
};
return athenaClient.startQueryExecution(params).promise();
};
return command;
};
在我的测试中,我正在模拟athena客户端并将其注入到函数中,我想测试使用作为输入发送的sqlCommand调用方法startQueryExecution。所以我一直在努力创建一个存根。
这是我的测试:
const AWS = require("aws-sdk");
const AWS_MOCK = require("aws-sdk-mock");
const sinon = require("sinon");
const expect = require("chai").expect;
describe("Executes a sql command in Athena", async done => {
process.env.ATHENA_DB = "default";
it("the sqlCommand is sent to startQueryExecution", async () => {
const SQL_COMMAND = "DROP TABLE IF EXISTS dataset_test PURGE;";
const athenaClient = {
startQueryExecution: params => ({})
};
const executeAthenaQueryCommand = require("../commands/executeAthenaQueryCommand").init(
{
athenaClient
}
);
sinon.stub(athenaClient, "startQueryExecution");
sinon.stub(executeAthenaQueryCommand, "execute");
const result = await executeAthenaQueryCommand.execute(SQL_COMMAND);
sinon.assert.calledWith(executeAthenaQueryCommand.execute, SQL_COMMAND);
const expectedResult = {
QueryString: SQL_COMMAND,
QueryExecutionContext: {
Database: "default"
}
};
sinon.assert.calledWith(athenaClient.startQueryExecution, expectedResult);
});
after(() => {});
});
但是我收到错误:
AssertError: expected startQueryExecution to be called with arguments
at Object.fail (node_modules/sinon/lib/sinon/assert.js:104:21)
at failAssertion (node_modules/sinon/lib/sinon/assert.js:61:16)
at Object.assert.(anonymous function) [as calledWith] (node_modules/sinon/lib/sinon/assert.js:86:13)
at Context.it (test/executeAthenaQueryCommand.spec.js:37:22)
at <anonymous>
有什么帮助吗?
答案
你几乎把它弄好了。纠正测试的一些注意事项是
在startQueryExecution存根中添加return
这是必须的,所以execute
函数正确执行以返回promise。
sinon.stub(athenaClient, "startQueryExecution").returns({ promise: () => Promise.resolve() });
删除执行方法的存根
这是我们想要测试的真实方法,我们在后续行中调用它,因此我们不能将其存根。
sinon.stub(executeAthenaQueryCommand, "execute"); // remove this
sinon.assert.calledWith(executeAthenaQueryCommand.execute, SQL_COMMAND); // remove this
所以最终的测试文件将是
describe("Executes a sql command in Athena", async done => {
...
it("the sqlCommand is sent to startQueryExecution", async () => {
...
sinon.stub(athenaClient, "startQueryExecution").returns({ promise: () => Promise.resolve() }); // add returns
const result = await executeAthenaQueryCommand.execute(SQL_COMMAND);
const expectedResult = {
QueryString: SQL_COMMAND,
QueryExecutionContext: {
Database: "default"
}
};
sinon.assert.calledWith(athenaClient.startQueryExecution, expectedResult);
});
after(() => {});
});
以上是关于使用javascript与Mocha和Sinon进行单元测试问题的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 sinon 和 Mocha 模拟对 mysql 查询 nodeJS 的 Promisify 调用?
单元/集成测试 Express REST API, mongoose, mocha, sinon, chai, supertest
markdown 节点单元测试备忘单:Mocha,Chai和Sinon