【发布时间】:2019-12-10 10:21:24
【问题描述】:
我正在尝试为 Nest 中的微服务编写 e2e 测试。我想我已经正确创建了我的 ProxyClient 以便向服务发出请求。
我在测试中想要做什么:
- 创建代理客户端
- 使用该客户端发送消息
- 让服务接收他们的消息
- 返回服务完成的消息处理结果
这是我目前所拥有的......看起来很接近,但我在运行测试时一直看到这个。
预期:真实, 收到:{"_isScalar": false, "operator": {"concurrent": Infinity, "project": [Function anonymous]}, "source": {"_isScalar": false, "_subscribe": [Function anonymous]} }
import { Inject } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ClientsModule, Transport, ClientProxy } from '@nestjs/microservices';
import * as request from 'supertest';
import { HeartbeatModule } from './../src/heartbeat.module';
import { HeartbeatService } from './../src/heartbeat.service';
describe('AppController (e2e)', () => {
let app;
let client: ClientProxy;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [HeartbeatModule, ClientsModule.register([{ name: 'HEARTBEAT_SERVICE', transport: Transport.TCP }])],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
client = app.get('HEARTBEAT_SERVICE');
await client.connect();
});
afterAll(async () => {
await client.close();
});
it('sends a level 1 heartbeat message to the HeartbeatService', async () => {
const response = client.send({"cmd": "heartbeat"}, {"type": 1});
expect(response).toBe(true);
});
});
【问题讨论】:
-
注意到这一点:HeartbeatController (e2e) › 向 HeartbeatService 发送 1 级心跳消息连接 ECONNREFUSED 127.0.0.1:3000
标签: typescript microservices e2e-testing nestjs