在单元测试中使用 ConfigService - NestJS

发布于 2025-01-19 06:47:43 字数 1816 浏览 4 评论 0原文

我有一个 Kafka ProducerService,如下所示:

@Injectable()
export class ProducerService implements OnModuleInit, OnApplicationShutdown {
  private logger = new Logger(ProducerService.name);
  public readonly topic: string;
  private readonly url: string;
  private readonly clientId: string;
  private readonly scramUsername: string;
  private readonly scramPassword: string;
  private kafka: Kafka;
  private producer: Producer;

  constructor(private config: ConfigService) {
    this.url = this.config.get<string>('kafka.brokerUrl');
    this.topic = this.config.get<string>('kafka.topic');
    this.clientId = this.config.get<string>('kafka.clientId');
    if(this.config.get<boolean>('kafka.scram.enabled') == true) {
      this.scramUsername = this.config.get<string>('kafka.scram.username');
      this.scramPassword = this.config.get<string>('kafka.scram.password');
    } else {
      this.scramUsername = "";
      this.scramPassword = "";
    }
    this.kafka = new Kafka({
      brokers: [this.url],
      clientId: this.clientId,
    });
    this.producer = this.kafka.producer();
  }

我在 KafkaModule 中导入 ConfigModule,这允许我在 中注入 ConfigService ProducerService 构造函数。

如何充分测试这项服务? 我正在我的 Producer.spec.ts 文件中尝试像这样基本的东西:

describe('ProducerService', () => {
  const config = new ConfigService()
  const client = new ProducerService(config)
  
  beforeEach(async () => {
    await client.onModuleInit();
  })

  it('Should connect', () => {
    expect(client).toBeDefined()
  })

但我始终无法将配置加载到测试对象中:

KafkaJSNonRetriableError: Failed to connect: broker at index 0 is invalid "undefined"

Wrt。配置,我使用的是 .yaml

I have a Kafka ProducerService which looks like this:

@Injectable()
export class ProducerService implements OnModuleInit, OnApplicationShutdown {
  private logger = new Logger(ProducerService.name);
  public readonly topic: string;
  private readonly url: string;
  private readonly clientId: string;
  private readonly scramUsername: string;
  private readonly scramPassword: string;
  private kafka: Kafka;
  private producer: Producer;

  constructor(private config: ConfigService) {
    this.url = this.config.get<string>('kafka.brokerUrl');
    this.topic = this.config.get<string>('kafka.topic');
    this.clientId = this.config.get<string>('kafka.clientId');
    if(this.config.get<boolean>('kafka.scram.enabled') == true) {
      this.scramUsername = this.config.get<string>('kafka.scram.username');
      this.scramPassword = this.config.get<string>('kafka.scram.password');
    } else {
      this.scramUsername = "";
      this.scramPassword = "";
    }
    this.kafka = new Kafka({
      brokers: [this.url],
      clientId: this.clientId,
    });
    this.producer = this.kafka.producer();
  }

I import the ConfigModule in my KafkaModule, which allows me to inject ConfigService in the ProducerService constructor function.

How can I test this service sufficiently?
I am trying something as basic as this in my producer.spec.ts file:

describe('ProducerService', () => {
  const config = new ConfigService()
  const client = new ProducerService(config)
  
  beforeEach(async () => {
    await client.onModuleInit();
  })

  it('Should connect', () => {
    expect(client).toBeDefined()
  })

but I keep failing to load configuration into the test object:

KafkaJSNonRetriableError: Failed to connect: broker at index 0 is invalid "undefined"

Wrt. config, I am using a .yaml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2025-01-26 06:47:43

您使用的是节点 16 吗?我遇到了同样的问题,我使用 nvm 切换到 Node 14。它现在可以工作了。

不知道实际问题是什么,但这可能会有所帮助。

Are you using Node 16? I had the same issue and I used nvm to switch to Node 14. It works now.

No idea what the actual issue was, but this may help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文