ganmacs / grpc_mock

Library for stubbing gRPC requests in Ruby.
MIT License
45 stars 19 forks source link

Usage experience of `with` #21

Open phunehehe opened 1 year ago

phunehehe commented 1 year ago

Hi! First of all thanks for making this library!

This is not really an issue... I'm not sure what I'm trying to suggest, just want to put this out here in case it helps someone somehow.

I found the following pattern (from the README) surprisingly frustrating to maintain:

GrpcMock.
  stub_request("/hello.hello/Hello").
  with(Hello::HelloRequest.new(msg: 'hi')).
  to_return(Hello::HelloResponse.new(msg: 'test'))

It reads nicely and it works well, but the wrinkle comes when the matcher doesn't work for some reason (most likely it's because I'm making changes to the request). Then it doesn't match and I get a confusing error saying real requests are not allowed. Even if I know what it's about and try to investigate, there's no way to inspect the request as written.

I find myself using the following pattern more:

GrpcMock.
  stub_request("/hello.hello/Hello").
  to_return do |request|
    expect(request).to eq(Hello::HelloRequest.new(msg: 'hi'))
    Hello::HelloResponse.new(msg: 'test')
  end

It's slightly longer, but when something goes wrong it tells me exactly where.