protoc-gen-moq is a plugin of the Google protocol buffers compiler protoc. It reads protobuf service definitions and generates a mocked service client structs. It generates its mocked struct using the matryer/moq template.
You first need to install the plugin by placing it in your $PATH
:
$ go install github.com/alexandrevilain/protoc-gen-moq@latest
Make sure that your $GOBIN
is in your $PATH
.
Then run it with protoc alongside the protoc-gen-go-grpc plugin:
$ protoc \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
--moq_out=. \
--moq_opt=paths=source_relative \
hello.proto
To run both unit and conformance tests run:
$ make test
I can't use the template package from moq for two reasons:
- the
template
package is inside theinternal
package which prevents external usage of the code inside. - the
template
package makes an extensive usage ofgo/types
which is not used by protogen.
To ensure the 100% compatibility between struct generated by protoc-gen-moq and moq I created conformance tests which generate the mock for https://github.com/grpc/grpc-go/blob/master/test/grpc_testing/test.proto. The rendered moq file is then checked using go vet which will report an error if the generated struct does not implement the interface methods.