如何使用摩卡柴sinon对下面的senario进行存根

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

我想为下面的代码添加模拟响应,我该怎么做?

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

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

从“ ./a.js”导入a是异步函数,我需要使用mocha chai编写单元测试。

unit-testing google-cloud-functions sinon sinon-chai
1个回答
0
投票

为了在Node.js中导出和导入,请遵循以下步骤:

a.js

const a= async () => { 
//logic 
   return result 
}
module.exports = { a: a };

云功能

const func = require('./a.js');
//Use the function like following: 
func.a()
© www.soinside.com 2019 - 2024. All rights reserved.