使用javascript与Mocha和Sinon进行单元测试问题

问题描述 投票:0回答:1

我正在尝试使用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>

有什么帮助吗?

javascript unit-testing mocha sinon
1个回答
1
投票

你几乎把它弄好了。纠正测试的一些注意事项是

在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(() => {});
});
© www.soinside.com 2019 - 2024. All rights reserved.