我正在尝试测试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;
}发布于 2021-01-05 13:11:18
我知道这很古老,但我想我应该回答这个问题,因为谷歌的人遇到了这个问题。
和@GrpcMethod()装饰器参数都是可选的。如果调用时没有第二个参数(例如' findOne '),Nest将根据将处理程序名转换为上camel大小写(例如,FindOne处理程序与FindOne rpc调用定义相关联),自动将.proto文件rpc方法与处理程序关联。这一点如下所示。
它说它是没有定义的,因为它是没有定义的。它正在寻找gprcFindAll,但是您已经将方法findAll命名为第二个param。
要么省略findAll
@GrpcMethod('HelloWorldGRPCService')
async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
const benchmarks = [];
return benchmarks;
}或更改第二个参数以匹配处理程序。
@GrpcMethod('HelloWorldGRPCService', 'GprcFindAll')
async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
const benchmarks = [];
return benchmarks;
}https://stackoverflow.com/questions/62066781
复制相似问题