首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在nestjs中模拟elasticsearch服务

无法在nestjs中模拟elasticsearch服务
EN

Stack Overflow用户
提问于 2018-11-28 07:02:16
回答 1查看 1.5K关注 0票数 1

当前行为

代码语言:javascript
复制
Nest can't resolve dependencies of the ElasticsearchService (?). Please make sure that the argument at index [0] is available in the ElasticsearchModule context.

预期行为

在测试模块中创建ElasticSearchService

带指令的问题的最小再现

代码语言:javascript
复制
import { Test, TestingModule } from '@nestjs/testing';
import { RepliesController } from './replies.controller';
import { ElasticsearchService, ElasticsearchModule } from '@nestjs/elasticsearch';
import { Client } from 'elasticsearch';

describe('Replies Controller', () => {
  let module: TestingModule;
  let elasticsearch: ElasticsearchService;

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [ElasticsearchModule],
      controllers: [RepliesController],
      components: [ElasticsearchService, {provide: Client, useValue: {}}],
    }).compile();

    elasticsearch = module.get<ElasticsearchService>(ElasticsearchService);
  });
  it('should be defined', () => {
    const controller: RepliesController = module.get<RepliesController>(RepliesController);
    expect(controller).toBeDefined();
  });
});

环境

代码语言:javascript
复制
   [Nest Information]
elasticsearch version : 0.1.2
common version        : 5.4.0
core version          : 5.4.0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-30 18:10:41

您需要使用测试模块公开的方法来覆盖提供程序,而不是在组件数组中这样做:

代码语言:javascript
复制
beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [ElasticsearchModule],
      controllers: [RepliesController]
    })
    .overrideProvider(ElasticsearchService)
    .useValue(/* your mock or whatever */)
    .compile();

在测试部分的NestJs文档中有这方面的例子。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53513865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档