在单元测试中使用 ConfigService - NestJS
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是节点 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.