Google cloud platform 如何使用sinon,mocha chai模拟以下代码的响应

Google cloud platform 如何使用sinon,mocha chai模拟以下代码的响应,google-cloud-platform,google-cloud-functions,google-cloud-storage,mocha.js,sinon,Google Cloud Platform,Google Cloud Functions,Google Cloud Storage,Mocha.js,Sinon,有人能帮我写一个测试场景示例吗 存储是一个库(谷歌云),最后在代码行下面会返回一个由文件名和日期组成的数组 函数abc(){ 常量文件=[]; files=wait storage.bucket(bucketName.getFiles(); 归还文件; } 以下是单元测试解决方案: index.ts: 从“@googlecloud/Storage”导入{Storage}; 常量存储=新存储(); 导出异步函数abc(){ const bucketName=“xxx dev”; const fil

有人能帮我写一个测试场景示例吗

存储
是一个库(谷歌云),最后在代码行下面会返回一个由文件名和日期组成的数组

函数abc(){
常量文件=[];
files=wait storage.bucket(bucketName.getFiles();
归还文件;
}

以下是单元测试解决方案:

index.ts

从“@googlecloud/Storage”导入{Storage};
常量存储=新存储();
导出异步函数abc(){
const bucketName=“xxx dev”;
const files=wait storage.bucket(bucketName.getFiles();
归还文件;
}
导出异步函数xyz(res){
const bucketName=“xxx dev”;
返回存储
.bucket(bucketName)
.file(res.fileName)
.createReadStream();
}
索引规范ts

从“/”导入{abc,xyz};
从“@googlecloud/Storage”导入{Storage};
从“sinon”进口sinon;
从“chai”导入{expect};
描述(“59373281”,()=>{
之后(()=>{
sinon.restore();
});
它(“abc应该通过”,异步()=>{
const getfilestub=sinon.stub()。解析([“文件1”,“文件2”);
const bucketsub=sinon.stub(Storage.prototype,“bucket”).callsFake(()=>{
将{getFiles:getfilestub}返回为任意值;
});
常量实际值=等待abc();
expect(实际).to.be.deep.eq([“file1”,“file2]”);
sinon.assert.calledWith(bucketsub,“xxx dev”);
sinon.assert.calledOnce(getfilestub);
});
它(“xyz应该通过”,异步()=>{
const fileStub=sinon.stub().returnsThis();
const createReadStreamStub=sinon.stub();
const bucketsub=sinon.stub(Storage.prototype,“bucket”).callsFake(()=>{
返回{
文件:fileStub,
createReadStream:createReadStreamStub,
}如有的话;
});
const mRes={fileName:“jestjs.pdf”};
等待xyz(mRes);
sinon.assert.calledWith(bucketsub,“xxx dev”);
assert.calledWith(fileStub,“jestjs.pdf”);
sinon.assert.calledOnce(createReadStreamStub);
});
});
100%覆盖率的单元测试结果:

59373281
✓ abc应该通过
✓ xyz应该通过
2次通过(46毫秒)
---------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
---------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
索引规范ts | 100 | 100 | 100 | 100 ||
index.ts | 100 | 100 | 100 | 100 ||
---------------|----------|----------|----------|----------|-------------------|
软件包版本:

"@google-cloud/storage": "^4.1.3",
"sinon": "^7.5.0",
"mocha": "^6.2.2",
"chai": "^4.2.0",

源代码:

我试图测试以下代码,我想我会关联,但仍然得到错误作为file.on not函数:-(

功能abc(请求、恢复){
const bucketName=“abc xyz”
const fileName=“Sample.json”
var file=storage.bucket(bucketName).file(文件名);
const myfile=file.createReadStream();
var缓冲区=“”;
myfile.on('data',函数(a){
缓冲器+=a;
}).on('end',function(){
console.log(缓冲区)
res.send(缓冲区)
});

}
这是我很久以来见过的最简单的问题!我也想知道如何为这种情况编写测试。谢谢@slideshowp2,我还有一个问题。如果我有一个函数,比如storage.bucket(bucketName).file(res.fileName).createReadStream().const bucketsub=sinon.stub(Storage.prototype,“bucket”).callsFake(()=>{return{file:{createReadStream:getFileStub};});当然@SlideShowp,谢谢:-)嗨@SlideShowp,你能帮我做下面的测试用例吗。@Tapas Ok,我会回答的。