【发布时间】:2020-09-15 21:23:11
【问题描述】:
我正在尝试测试 GRPC 方法,但在 Jest 中运行时出现此错误: TypeError:无法读取未定义的属性“gprcFindAll”
这是我引用的文档: https://docs.nestjs.com/microservices/grpc
这是我的控制器:
@Controller('benchmarks')
export class BenchmarksController {
@GrpcMethod('HelloWorldGRPCService', 'findAll')
async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
const benchmarks = [];
return benchmarks;
}
}
这是我的 Jest 测试:
describe('Benchmarks Controller', () => {
let controller: BenchmarksController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [BenchmarksController]
}).compile();
controller = module.get<BenchmarksController>(BenchmarksController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
it('GRPC Should Find All', async function() {
const result = await controller.gprcFindAll();
console.log(result);
});
});
这是我的 Proto 3 文件:
syntax = "proto3";
package helloWorldGRPC;
service HelloWorldGRPCService {
rpc findAll () returns (repeated Benchmarks);
}
message Benchmarks {
string trans_id = 1;
string protocol = 2;
string database = 3;
Timestamp updated_at = 4;
Timestamp created_at = 5;
repeated Action actions = 6;
}
message Action {
string trans_id = 1;
int32 payload_length = 2;
string payload = 3;
string status = 4;
Timestamp updated_at = 5;
Timestamp created_at = 6;
}
【问题讨论】:
-
您的控制器中还有其他内容吗?我能想到任何类型的提供者未定义的唯一原因是,如果 Nest 无法首先创建提供者(或在这种情况下为控制器)。此处未显示的任何其他依赖项?