如何使用Mocha Chai和sinon编写以下功能的单元测试用例

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

我无法在下面的代码库中编写测试用例,我可以如下所示对模拟数据进行存根处理,但在fileData.on处失败

const bucketStub = sinon.stub(Storage.prototype, "bucket").callsFake(() => {
      return {
        file: fileStub,
        createReadStream: createReadStreamStub,
      } as any;
    });

下面是我尝试使用mocha chai和sinon编写测试用例的代码

    function abc(req, res){
       const bucketName = "abc-xyz"
        const fileName = "Sample.json"
        var file = storage.bucket(bucketName).file(fileName);
        const myfile = file.createReadStream();
        var  buffer = '';
        myfile.on('data', function(a) {
          buffer += a;
        }).on('end', function() {
          console.log(buffer)
           res.send(buffer)
        });
 }
google-cloud-platform google-cloud-functions sinon sinon-chai
1个回答
0
投票

这里是单元测试解决方案:

index.ts

import { Storage } from "@google-cloud/storage";
const storage = new Storage();

export function abc(req, res) {
  const bucketName = "abc-xyz";
  const fileName = "Sample.json";
  const file = storage.bucket(bucketName).file(fileName);
  const myfile = file.createReadStream();
  let buffer = "";
  myfile
    .on("data", function(a) {
      buffer += a;
    })
    .on("end", function() {
      console.log(buffer);
      res.send(buffer);
    });
}

index.spec.ts

import { abc } from ".";
import sinon from "sinon";
import { Storage } from "@google-cloud/storage";

describe("59405183", () => {
  beforeEach(() => {
    sinon.restore();
  });
  afterEach(() => {
    sinon.restore();
  });
  it("should pass", () => {
    const mReq = {};
    const mRes = {
      send: sinon.stub(),
    };
    const onStub = sinon.stub();
    const onDataStub = onStub
      .withArgs("data")
      .yields("sinon")
      .returnsThis();
    const onEndStub = onStub.withArgs("end").yields();

    const readStreamStub = { on: onStub };
    const stubs = {
      file: sinon.stub().returnsThis(),
      createReadStream: sinon.stub().returns(readStreamStub),
    };
    const bucketStub = sinon.stub(Storage.prototype, "bucket").callsFake(() => stubs as any);
    abc(mReq, mRes);
    sinon.assert.calledWith(bucketStub, "abc-xyz");
    sinon.assert.calledWith(stubs.file, "Sample.json");
    sinon.assert.calledOnce(stubs.createReadStream);
    sinon.assert.calledWith(onDataStub, "data", sinon.match.func);
    sinon.assert.calledWith(onEndStub, "end", sinon.match.func);
    sinon.assert.calledWith(mRes.send, "sinon");
  });
});

单元测试结果覆盖率100%:

  59405183
sinon
    ✓ should pass


  1 passing (28ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |      100 |      100 |      100 |      100 |                   |
 index.spec.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |      100 |      100 |                   |
---------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59405183%2659497269

© www.soinside.com 2019 - 2024. All rights reserved.