Unit testing 如何使用mocha chai sinon存根下面的senario

Unit testing 如何使用mocha chai sinon存根下面的senario,unit-testing,google-cloud-functions,sinon,sinon-chai,Unit Testing,Google Cloud Functions,Sinon,Sinon Chai,我想为下面的代码存根模拟响应,我怎么做 从“@googlecloud/Storage”导入{Storage}; 常量存储=新存储(); 从“/a.js”导入文件 导出功能abc(请求、回复){ a()。然后(结果->{ const bucketName=result.bucketName; const fileName=“Sample.json”; const file=storage.bucket(bucketName).file(result.fileName); const myfile

我想为下面的代码存根模拟响应,我怎么做

从“@googlecloud/Storage”导入{Storage};
常量存储=新存储();
从“/a.js”导入文件
导出功能abc(请求、回复){
a()。然后(结果->{
const bucketName=result.bucketName;
const fileName=“Sample.json”;
const file=storage.bucket(bucketName).file(result.fileName);
const myfile=file.createReadStream();
设buffer=“”;
我的文件
.on(“数据”,功能(a){
缓冲器+=a;
})
.on(“结束”,函数(){
日志(缓冲区);
资源状态(200)。发送(缓冲区);
});
});

}
按照以下步骤在Nodejs中导出和导入:

a、 js

云函数

const func = require('./a.js');
//Use the function like following: 
func.a()

是否正确导出a.js中的模块?看看这个。让我知道是否适合你。否则,我可以进一步帮助您。是的,函数const a=async()=>{//logic return result}module.exports=a;您好@Nibrass,我已经按照上面所示进行了尝试,并对结果进行了stubbed,但是我得到了下面的错误类型error:a.a(…)。然后不是一个函数——我对sinon.stub(a,“a”).callsFake(()=>{return{resultStub};})进行了stubbed的方式@Tapas Vishal,你能用全部代码更新帖子吗?你认为问题在于sinon库的工作方式吗?我知道sinon的工作方式,但问题是如何模拟a()。然后(result=>{})我将从一个函数中得到的结果。你是按以下方式做的吗,func.a()。然后(result=>{})?是的,您好@slideshowp2您能帮我解决上述问题吗
const func = require('./a.js');
//Use the function like following: 
func.a()