Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/qawatake/grpcmock

grpcmock provides a mock gRPC server from a generated gRPC client code
https://github.com/qawatake/grpcmock

go golang

Last synced: about 2 months ago
JSON representation

grpcmock provides a mock gRPC server from a generated gRPC client code

Awesome Lists containing this project

README

        

# grpcmock

[![Go Reference](https://pkg.go.dev/badge/github.com/qawatake/grpcmock.svg)](https://pkg.go.dev/github.com/qawatake/grpcmock)
[![test](https://github.com/qawatake/grpcmock/actions/workflows/test.yaml/badge.svg)](https://github.com/qawatake/grpcmock/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/qawatake/grpcmock/graph/badge.svg?token=iBMN98cHlc)](https://codecov.io/gh/qawatake/grpcmock)

grpcmock provides a mock gRPC server from a generated gRPC client code.

This package currently supports unary RPCs only.

```go
func TestClient(t *testing.T) {
ts := grpcmock.NewServer(t)
conn := ts.ClientConn()
client := hello.NewGrpcTestServiceClient(conn)

// Register a mock response.
helloRPC := grpcmock.Register(ts, "/hello.GrpcTestService/Hello", hello.GrpcTestServiceClient.Hello).
Response(&hello.HelloResponse{
Message: "Hello, world!",
})
ts.Start()

ctx := context.Background()
res, _ := client.Hello(ctx, &hello.HelloRequest{Name: "qawatake"})

if res.Message != "Hello, world!" {
t.Errorf("unexpected response: %s", res.Message)
}
{
// Retrieve the request(s)
got := helloRPC.Requests()[0].Body
if got.Name != "qawatake" {
t.Errorf("unexpected request: %v", got)
}
}
}
```

## References

- [k1LoW/grpcstub]: provides a stub gRPC server from protobuf files (not from generated gRPC client code)

[k1LoW/grpcstub]: https://github.com/k1LoW/grpcstub