https://github.com/alexandrevilain/protoc-gen-moq
protoc plugin which generates client structs mocks
https://github.com/alexandrevilain/protoc-gen-moq
golang grpc mock protobuf protocol-buffers
Last synced: 4 months ago
JSON representation
protoc plugin which generates client structs mocks
- Host: GitHub
- URL: https://github.com/alexandrevilain/protoc-gen-moq
- Owner: alexandrevilain
- License: mit
- Created: 2021-12-23T12:20:29.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-01T13:12:25.000Z (over 3 years ago)
- Last Synced: 2025-08-22T10:41:42.545Z (10 months ago)
- Topics: golang, grpc, mock, protobuf, protocol-buffers
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protoc-gen-moq
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](https://github.com/matryer/moq) template.
## How to use it ?
You first need to install the plugin by placing it in your `$PATH`:
```bash
$ 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:
```bash
$ 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
```
## Running tests
To run both unit and conformance tests run:
```bash
$ make test
```
## Why not using the template package of github.com/matryer/moq ?
I can't use the template package from [moq](https://github.com/matryer/moq) for two reasons:
- the `template` package is inside the `internal` package which prevents external usage of the code inside.
- the `template` package makes an extensive usage of `go/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](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.