【问题标题】:How to Mock a bulk method of elastic search searvice如何模拟弹性搜索服务的批量方法
【发布时间】:2022-01-17 19:42:36
【问题描述】:

我正在为我的 searchService 做测试,我意识到用于向 elasticSearch db 插入数据的批量方法有一些实习操作。

beforeEach(async () => {
const moduleRef: TestingModule = await Test.createTestingModule({
  providers: [
    SearchService,
    {
      provide: ElasticsearchService,
      useValue: {
        bulk: jest.fn(),
        count: jest.fn(),
        helpers: {
          scrollSearch: jest.fn(),
        },
        search: jest.fn(),
      },
    },
    {
      provide: PrismaService,
      useValue: {
        skus: {
          findMany: jest.fn().mockResolvedValueOnce(findManyMock),
          findUnique: jest.fn().mockResolvedValue({}),
          update: jest.fn().mockResolvedValue({}),
          create: jest.fn().mockResolvedValue({}),
          delete: jest.fn().mockResolvedValue({}),
        },
      },
    },
  ],
}).compile();

调用的批量方法:

for await (const chunk of arrayOfBulkBodiesInChunks) {
  const bulkBody = chunk.flatMap((doc) => [
    { index: { _index: this.index } },
    doc,
  ]);

  await this.elasticsearchService.bulk({
    refresh: true,
    body: bulkBody,
  });
}

我正在寻求帮助,为批量操作编写一个模拟。

【问题讨论】:

    标签: elasticsearch jestjs nestjs prisma elasticsearch-jest


    【解决方案1】:

    ,您可以模拟您的依赖项或提供程序,试试这个:

    • 在/ElasticsearchService所在的同一个文件目录下,创建一个名为mocks的文件,例如:ElasticsearchService本地化在/service/ElasticsearchService.ts,创建一个文件/service/mocks/ElasticsearchService.ts
    • 模拟 ElasticsearchService.ts 会将服务导出到模拟

        export const ElasticsearchService = jest.fn().mockReturnValue({
         //Name here the function to mock
         bulk: jest.fn().mockReturnValue({
        bulk: jest.fn(),
        count: jest.fn(),
        helpers: {
          scrollSearch: jest.fn(),
        },
        search: jest.fn(),
      })

    然后在您的测试开始时。

    jest.mock('/file/ElasticsearchService') //Type here the exact ubication of your ElasticsearchService, jest automatically will implement the file with the same name in the folder __mocks__
    
    ```beforeEach(async () => {
    const moduleRef: TestingModule = await Test.createTestingModule({
      providers: [
        SearchService,
        {
          provide: ElasticsearchService,
          useValue: {
            bulk: jest.fn(),
            count: jest.fn(),
            helpers: {
              scrollSearch: jest.fn(),
            },
            search: jest.fn(),
          },
        },
        {
          provide: PrismaService,
          useValue: {
            skus: {
              findMany: jest.fn().mockResolvedValueOnce(findManyMock),
              findUnique: jest.fn().mockResolvedValue({}),
              update: jest.fn().mockResolvedValue({}),
              create: jest.fn().mockResolvedValue({}),
              delete: jest.fn().mockResolvedValue({}),
            },
          },
        },
      ],
    }).compile();```

    希望你能解决它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多