如何检查gRPC端点返回的值?

时间:2020-10-03 19:06:54

标签: go protocol-buffers

我正在用Go编写实现gRPC API的应用程序。

以下是ProtoBuff规范,它描述了我端点的响应消息:

rpc MyEndpoint(RequestMessage) returns (ResponseMessage) {
  option (google.api.http) = {
    post: "/MyEndpoint"
    body: "*"
  };
}

message ResponseMessage {
  message SubMessage {
    int32 a = 1;
    map<string, int32> b = 2;
  }
  string c = 1;
  map<string, SubMessage> d = 2;
}

我正在编写一个测试用例,以验证来自端点的响应是否符合我的预期。 这是我要执行的代码:

expectedResponse := ResponseMessage{
    C: "string1",
    D: map[string]*ResponseMessage_SubMessage{
        "string2": &ResponseMessage_SubMessage{
            A: 1,
            B: map[string]int32{
                "string3": 2,
            },
        },
    },
}
fmt.Println("responseFromEndpoint =  ", responseFromEndpoint)
fmt.Println("expectedResponse     =  ", expectedResponse)
assert.Equal(t, expectedResponse, responseFromEndpoint) // This is line #230 in file my_test_file.go

运行此测试用例时,得到以下输出:

responseFromEndpoint = map[c:string1 d:map[string2:map[a:1 b:map[string3:2]]]]
expectedResponse     = {{{} [] [] <nil>} 0 [] string1 map[string2:a:1  b:{key:"string3"  value:2}]}

测试用例失败,如下所示:

MyTestClass/MyTestCase: my_test_file.go:230:
        Error Trace:    my_test_file.go:230
        Error:          Not equal:
                        expected: ResponseMessage(ResponseMessage{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(nil)}, sizeCache:0, unknownFields:[]uint8(nil), Name:"string1", SubMessages:map[string]*ResponseMessage_SubMessage{"string2":(*ResponseMessage_SubMessage)(0xc000098180)}})
                        actual  : map[string]interface {}(map[string]interface {}{"c":"string1", "d":map[string]interface {}{"string2":map[string]interface {}{"a":1, "b":map[string]interface {}{"string3":2}}}})

如何修改测试用例,以确保端点可以使用期望的数据进行响应?

0 个答案:

没有答案