我正在对我的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,
});
}我在请求帮助为批量操作编写一个模拟程序。
发布于 2021-12-14 20:01:29
Hi,您可以模拟您的依赖项或提供程序,尝试如下:
在同一个文件目录下,创建一个名为/service/mocks/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 =等待Test.createTestingModule({
提供者:[
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();`
希望你能解决它,
https://stackoverflow.com/questions/70349833
复制相似问题