【问题标题】:Unit test issue with Mocha and Sinon using javascript使用 JavaScript 的 Mocha 和 Sinon 的单元测试问题
【发布时间】:2019-02-22 08:35:01
【问题描述】:

我正在尝试使用 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 客户端并将其注入函数中,并且我想测试方法 startQueryExecution 是否使用作为输入发送的 sqlCommand 调用。所以我一直在尝试在它上面创建一个存根。

这是我的测试:

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>

有什么帮助吗?

【问题讨论】:

  • 您正在存根 execute 方法,但没有指定它应该返回/解决什么。看来你可能不想存根 execute 如果它正在做一些处理创建一个参数发送到 startQueryExecution

标签: javascript unit-testing mocha.js sinon


【解决方案1】:

你几乎说对了。纠正测试的一些注意事项是

在 startQueryExecution 存根中添加返回

这是必须的,所以execute 函数被正确执行以返回承诺。

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(() => {});
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2021-04-26
    • 2019-02-03
    • 2021-09-27
    • 2023-04-09
    • 2018-11-05
    相关资源
    最近更新 更多