首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Nest和Jest测试GRPC?

如何用Nest和Jest测试GRPC?
EN

Stack Overflow用户
提问于 2020-05-28 14:08:52
回答 1查看 2K关注 0票数 2

我正在尝试测试GRPC方法,但是在Jest: TypeError:无法读取未定义的属性'gprcFindAll‘时,我得到了这个错误

这是我所参考的文档:https://docs.nestjs.com/microservices/grpc

这是我的控制器:

代码语言:javascript
复制
@Controller('benchmarks')
export class BenchmarksController {

    @GrpcMethod('HelloWorldGRPCService', 'findAll')
    async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
        const benchmarks = [];
        return benchmarks;
    }
}

这是我的Jest测试:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-05 13:11:18

我知道这很古老,但我想我应该回答这个问题,因为谷歌的人遇到了这个问题。

和@GrpcMethod()装饰器参数都是可选的。如果调用时没有第二个参数(例如' findOne '),Nest将根据将处理程序名转换为上camel大小写(例如,FindOne处理程序与FindOne rpc调用定义相关联),自动将.proto文件rpc方法与处理程序关联。这一点如下所示。

它说它是没有定义的,因为它是没有定义的。它正在寻找gprcFindAll,但是您已经将方法findAll命名为第二个param。

要么省略findAll

代码语言:javascript
复制
@GrpcMethod('HelloWorldGRPCService')
async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
    const benchmarks = [];
    return benchmarks;
}

或更改第二个参数以匹配处理程序。

代码语言:javascript
复制
@GrpcMethod('HelloWorldGRPCService', 'GprcFindAll')
async gprcFindAll(metadata?: any): Promise<Benchmarks[]> {
    const benchmarks = [];
    return benchmarks;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62066781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档