如何用玩笑嘲笑S3?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用玩笑嘲笑S3?相关的知识,希望对你有一定的参考价值。

我正在尝试编写要上传的测试代码。但我并没有低估如何正确使用jest.mock('aws-sdk')

export class S3Service {
  private readonly s3: S3;
  private readonly bucket: string;
  constructor(private readonly configService: ConfigService) {
    this.s3 = new S3({
      accessKeyId: this.configService.get(''),
      secretAccessKey: this.configService.get(''),
      region: this.configService.get(''),
    });
    this.bucket = this.configService.get('');
  }
async upload(name: string, contentType: string, buffer: Buffer): Promise<string> {
    const upload = await this.s3.upload({params...}).promise();
    return upload;
  }
}
答案

这里是单元测试解决方案:

s3Service.ts

import { S3 } from 'aws-sdk';

export class S3Service {
  private readonly s3: S3;
  private readonly bucket: string;
  constructor(private readonly configService) {
    this.s3 = new S3({
      accessKeyId: this.configService.get(''),
      secretAccessKey: this.configService.get(''),
      region: this.configService.get(''),
    });
    this.bucket = this.configService.get('');
  }
  public async upload(name: string, contentType: string, buffer: Buffer): Promise<any> {
    const bucket = this.bucket;
    const params = { Bucket: bucket, Key: 'key', Body: buffer };
    const upload = await this.s3.upload(params).promise();
    return upload;
  }
}

s3Service.test.ts

import { S3Service } from './s3Service';

const mS3Instance = {
  upload: jest.fn().mockReturnThis(),
  promise: jest.fn(),
};

jest.mock('aws-sdk', () => {
  return { S3: jest.fn(() => mS3Instance) };
});

describe('61830632', () => {
  it('should upload correctly', async () => {
    const configService = {
      get: jest
        .fn()
        .mockReturnValueOnce('accessKeyId')
        .mockReturnValueOnce('secretAccessKey')
        .mockReturnValueOnce('us-east')
        .mockReturnValueOnce('bucket-dev'),
    };
    mS3Instance.promise.mockResolvedValueOnce('fake response');
    const s3Service = new S3Service(configService);
    const actual = await s3Service.upload('name', 'contentType', Buffer.from('ok'));
    expect(actual).toEqual('fake response');
    expect(mS3Instance.upload).toBeCalledWith({ Bucket: 'bucket-dev', Key: 'key', Body: Buffer.from('ok') });
  });
});

具有100%覆盖率的单元测试结果:

 PASS  stackoverflow/61830632/s3Service.test.ts (11.362s)
  61830632
    ✓ should upload correctly (6ms)

--------------|---------|----------|---------|---------|-------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
--------------|---------|----------|---------|---------|-------------------
All files     |     100 |      100 |     100 |     100 |                   
 s3Service.ts |     100 |      100 |     100 |     100 |                   
--------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.738s

以上是关于如何用玩笑嘲笑S3?的主要内容,如果未能解决你的问题,请参考以下文章

如何用玩笑模拟组件中使用的“嵌套”角度服务

如何用笑话模拟 S3?

如何用 ViewPager 中的另一个片段替换 Android 片段?

如何用几个逻辑“或”编写 s3 条件存储桶策略?

Android NavController:如何用相同的动作打开相同的片段

如何用 Android 数据绑定替换 androidx.fragment.app.FragmentContainerView 中的片段