【发布时间】:2019-08-30 21:07:22
【问题描述】:
我想模拟一个 GCP 存储桶,但 Typescript 因为打字而大喊大叫。
这是我要测试的类的摘录:
private storage = new Storage({
projectId: 'PROJECT_NAME',
keyFilename: env.gcpKeyFilename,
});
get bucket() {
return this.storage.bucket('fundee-assets');
}
private async _downloadFromBucket(name) {
const file = this.bucket.file(`${name}`);
const destination = `${name}`.split('/').pop();
await file.download({ destination, validation: false });
return destination;
}
我不想开玩笑地模拟 GCP 存储桶的一部分。所以我尝试了:
jest.spyOn(service, 'bucket', 'get').mockImplementationOnce(
()=>{
return {
file(name){
return {
download(dest, validation){
return dest;
}
};
}
}
}
)
但是 typescript 大喊大叫,因为它没有 GCP Bucket 类型的所有属性:
Type '{ file(name: string): { download(dest: any, validation: any): any; }; }' is missing the following properties from type 'Bucket': name, storage, acl, iam, and 52 more.
关于如何绕过这个或者我做的测试完全错误的任何想法?
【问题讨论】:
标签: typescript unit-testing google-cloud-platform jestjs