Microservices Nest JS微服务TCP E2E测试
有人知道如何为nest微服务编写E2E测试吗?给这个密码 梅因酒店Microservices Nest JS微服务TCP E2E测试,microservices,e2e-testing,nestjs,Microservices,E2e Testing,Nestjs,有人知道如何为nest微服务编写E2E测试吗?给这个密码 梅因酒店 import { NestFactory } from '@nestjs/core'; import { Transport } from '@nestjs/microservices'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.createMicroservic
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
});
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
app.controller.ts
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
@Controller()
export class MathController {
@MessagePattern({ cmd: 'sum' })
accumulate(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
}
}
这应该适合您:
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { ClientsModule, Transport, ClientProxy } from '@nestjs/microservices';
import * as request from 'supertest';
import { Observable } from 'rxjs';
describe('Math e2e test', () => {
let app: INestApplication;
let client: ClientProxy;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
MathModule,
ClientsModule.register([
{ name: 'MathService', transport: Transport.TCP },
]),
],
}).compile();
app = moduleFixture.createNestApplication();
app.connectMicroservice({
transport: Transport.TCP,
});
await app.startAllMicroservicesAsync();
await app.init();
client = app.get('MathService');
await client.connect();
});
afterAll(async () => {
await app.close();
client.close();
});
it('test accumulate', done => {
const response: Observable<any> = client.send(
{ cmd: 'sum' },
{ data: [1, 2, 3] },
);
response.subscribe(sum=> {
expect(sum).toBe(6);
done();
});
});
});
从'@nestjs/common'导入{INestApplication};
从'@nestjs/testing'导入{Test,TestingModule};
从'@nestjs/microservices'导入{ClientsModule,Transport,ClientProxy};
根据“supertest”的请求导入*;
从“rxjs”导入{Observable};
描述('数学e2e测试',()=>{
let-app:不适用;
让客户端:ClientProxy;
之前(异步()=>{
常量ModuleTexture:TestingModule=Wait Test.createTestingModule({
进口:[
数学模块,
ClientsModule.register([
{name:'MathService',传输:transport.TCP},
]),
],
}).compile();
app=moduleTexture.createNestApplication();
app.connectMicroservice({
传输:transport.TCP,
});
等待app.startAllMicroservicesAsync();
等待app.init();
client=app.get('MathService');
等待client.connect();
});
毕竟(异步()=>{
等待app.close();
client.close();
});
它('测试累积',完成=>{
常量响应:可观察=client.send(
{cmd:'sum'},
{数据:[1,2,3]},
);
响应。订阅(总和=>{
期望(总和)为(6);
完成();
});
});
});
是的,就是这样。一个简单的更改:const-response:Observable=client.send({cmd:'sum'}[1,2,3],);只发送数组而不发送对象这太棒了!令我惊讶的是,文档中没有这方面的内容。