Home > Net >  Mocking proto.Message in Go
Mocking proto.Message in Go

Time:05-24

I wrote a function that takes a list of proto.Message objects. Looking at the documentation, it seems like proto.Message wraps protoreflect.ProtoMessage which contains a single function, ProtoReflect() Message. Looking at the documentation for Message, it implements a number of other functions that return types referenced by the protoreflect package.

It seems that attempting to create a mock proto.Message would be a lot more work that it's worth but I don't want to go through the whole process of creating a protobuf file, compiling it and referencing it just for unit testing.

Is there another way I can create a mock proto.Message object?

CodePudding user response:

After looking into this further, I decided that the best thing to do would be to create an actual protobuf message:

syntax = "proto3";
package my.package;

option go_package = "path/to/my/package"; // golang

message TestRecord {
    string key = 1;
    string value = 2;
}

and compile it into a Golang file and then modify it for my own purposes:

$ protoc --go_out=./gopb/ --go_opt=paths=source_relative *.proto

Once the test file has been created, I can delete it or save it as a record.

  • Related