Skip to main content

Example Go consumer project

Source Code

https://github.com/pact-foundation/pact-plugins/tree/main/examples

This example project has a consumer stub for the area calculator service call and exercises it in a Pact test. It tests the following interaction from the proto file:

  rpc calculate (ShapeMessage) returns (AreaResponse) {}

You need to install the Pact Go library.

gRPC plugin

To run the test in this project, it requires the gRPC plugin to be installed. See the documentation on that plugin.

Generated gRPC stub

To generate the Go code for the proto file, you need to install the Protobuf compiler (protoc), and install the Go protobuf and grpc protoc plugins, and then run protoc --go_out=. --go-grpc_out=. --proto_path ../proto ../proto/area_calculator.proto.

Test method

The test method test_proto_client first sets up the interaction using the Pact DSL, then sets up a gRPC mock server to use. The generated stub structs are then used to send the ShapeMessage to the mock server, and an AreaResponse message is received back. This is then validated.