Google cloud platform 谷歌云功能的单元测试

Google cloud platform 谷歌云功能的单元测试,google-cloud-platform,google-cloud-functions,mocha.js,chai,sinon,Google Cloud Platform,Google Cloud Functions,Mocha.js,Chai,Sinon,我试图为谷歌云功能编写单元测试,参考谷歌云平台单元测试文档,但无法理解在使用谷歌云存储时如何模拟。下面是我试图为其编写单元测试的示例代码: exports.listFiles = async(req, res) => { let bucketName = req.body.bucket || req.query.bucket // Import the Google Cloud Storage library const {Storage} = require('@googl

我试图为谷歌云功能编写单元测试,参考谷歌云平台单元测试文档,但无法理解在使用谷歌云存储时如何模拟。下面是我试图为其编写单元测试的示例代码:

exports.listFiles = async(req, res) => {
  let bucketName = req.body.bucket || req.query.bucket

  // Import the Google Cloud Storage library
  const {Storage} = require('@google-cloud/storage');

  // Initiate a Storage client
  const storage = new Storage();

  // List files in the bucket and store their name in the array called 'result'
  const [files] = await storage.bucket(bucketName).getFiles();
  let result = [];
  files.forEach(file => {
    result.push(file.name);
  });

  // Send the result upon a successful request
  res.status(200).send(result);
};

没有用于GCS服务脱机使用的模拟器

一些开发人员创建了自己的模拟类来模拟Google云存储

此外,建议离线开发使用HTTP请求接收器库:

在python中,可以实现更多的单元测试上下文:

您可以在此处找到更多文档:


为了简化测试步骤,我在自己的类中提取API调用,并编写了一个可以在测试期间模拟的接口


我的目标首先是测试我的业务逻辑,而不是API调用,我假设它能按预期工作。

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

index.js:

exports.listFiles=异步请求,res=>{ const bucketName=req.body.bucket | | req.query.bucket; 常数{Storage}=require@google-云/存储; 常量存储=新存储; const[files]=await storage.bucketbucketName.getFiles; 常量结果=[]; files.forEachfile=>{ result.pushfile.name; }; res.status200.sendresult; }; index.spec.js:

const sinon=要求; 常数{Storage}=require@google-云/存储; const{listFiles}=require。; 描述文件,=>{ 之后=>{ 恢复; }; 它应该通过,异步=>{ const mFiles=[{name:sinon},{name:mocha},{name:chai}]; const getfilestub=sinon.stub.resolves[mFiles]; const bucketStub=sinon.stubStorage.prototype,bucket.callsFake=>{getFiles:getfilestub}; constmreq={body:{bucket:xxx dev},query:{bucket:xxx release}; const mRes={status:sinon.stub.returnsThis,send:sinon.stub}; 等待列表文件MREQ、mRes; sinon.assert.calledWithbucketStub,xxx开发; sinon.assert.calledOncegetFilesStub; sinon.assert.calledWithmRes.status,200; sinon.assert.calledWithmRes.send,[sinon,mocha,chai]; }; }; 单元测试结果和覆盖率报告:

列表文件 ✓ 应该通过 1通过12毫秒 --------|-----|-----|-----|-----|----------| 文件|%Stmts |%Branch |%Funcs |%Lines |未覆盖的行| --------|-----|-----|-----|-----|----------| 所有文件| 100 | 50 | 100 | 100 || index.js | 100 | 50 | 100 | 100 | 2| index.spec.js | 100 | 100 | 100 | 100 || --------|-----|-----|-----|-----|----------| 源代码:

const gcs = new Storage()

gcs.interceptors.push({
  request: reqOpts => {
    console.log(reqOpts)
    return reqOpts
  }
})
This will log all HTTP requests we make in the format the request module accepts.

Also, if you pass customEndpoint: true, it will skip the authentication step:

const Storage = require('@google-cloud/storage')
const gcs = new Storage({ customEndpoint: true })

  ```def test_read_sql(self):
    storage_client = mock.create_autospec(storage.Client)
    mock_bucket = mock.create_autospec(storage.Bucket)
    mock_blob = mock.create_autospec(storage.Blob)
    mock_bucket.return_value = mock_blob
    storage_client.get_bucket.return_value = mock_bucket
    mock_bucket.get_blob.return_value = mock_blob