【问题标题】:e2e testing a microservice in Neste2e 在 Nest 中测试微服务
【发布时间】:2019-12-10 10:21:24
【问题描述】:

我正在尝试为 Nest 中的微服务编写 e2e 测试。我想我已经正确创建了我的 ProxyClient 以便向服务发出请求。

我在测试中想要做什么:

  1. 创建代理客户端
  2. 使用该客户端发送消息
  3. 让服务接收他们的消息
  4. 返回服务完成的消息处理结果

这是我目前所拥有的......看起来很接近,但我在运行测试时一直看到这个。

预期:真实, 收到:{"_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


【解决方案1】:

好的,所以我在 beforeEach 中遗漏了一些东西......这是工作代码......

import { INestApplication } 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';
import { Observable } from 'rxjs';

describe('HeartbeatController (e2e)', () => {
  let app: INestApplication;
  let client: ClientProxy;

  beforeAll(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [
        HeartbeatModule,
        ClientsModule.register([
          { name: 'HEARTBEAT_SERVICE', transport: Transport.TCP },
        ]),
      ],
    }).compile();

    app = moduleFixture.createNestApplication();

    app.connectMicroservice({
      transport: Transport.TCP,
    });

    await app.startAllMicroservicesAsync();
    await app.init();

    client = app.get('HEARTBEAT_SERVICE');
    await client.connect();
  });

  afterAll(async () => {
    await app.close();
    client.close();
  });

  it('sends a level 1 heartbeat message to the HeartbeatService', done => {
    const response: Observable<any> = client.send(
      { cmd: 'heartbeat' },
      { type: 1 },
    );

    response.subscribe(json => {
      expect(Date.parse(json.now)).toBeLessThanOrEqual(new Date().getTime());
      expect(json.originalRequest.type).toBe(1);

      done();
    });
  });

  it('sends a level 2 heartbeat message to the HeartbeatService', done => {
    const requestJson = {
      type: 2,
      startTime: 12345,
      endTime: 67890,
      messagesSent: 22,
    };
    const response: Observable<any> = client.send(
      { cmd: 'heartbeat' },
      requestJson,
    );

    response.subscribe(json => {
      expect(Date.parse(json.now)).toBeLessThanOrEqual(new Date().getTime());
      expect(json.messagesReceived).toBe(22);
      expect(json.originalRequest.type).toBe(2);
      expect(json.originalRequest.startTime).toBe(12345);
      expect(json.originalRequest.endTime).toBe(67890);
      expect(json.originalRequest.messagesSent).toBe(22);

      done();
    });
  });

});

我收到一个开玩笑的错误“测试运行完成后一秒钟没有退出开玩笑。”。我可能会忽略它,但对如何解决它的任何想法持开放态度:)

编辑:感谢 Jay McDoniel,我用完整的工作代码更新了我的答案。

【讨论】:

  • 哦!您可以通过向可观察对象添加回调并调用它来修复未退出错误。所以在你it 行上,第二个参数应该是(done),在你所有的断言之后,你应该调用done()。您还应该考虑为您的可观察对象使用观察者而不是回调,因为将来不推荐使用回调。 Here's a gist 那种过头了。我还需要更新它
  • 你也可以创建函数async并使用await const json = response.toPromise()而不是完成,这样你就不需要doneit("{cmd: 'sum'}", async () =&gt; { const result = await client.send({cmd: 'sum'}, [1, 2, 3]).toPromise(); expect(result).toEqual(6); });
猜你喜欢
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2019-08-25
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多