Behaviour for mock GRPC.Adapter.Gun

Hi.

I’m trying to test a module that performs gRPC calls using the elixir-grpc library.

Such library allows to inject the underlying HTTP client which by default is GRPC.Adapter.Gun.

My idea is to create a mock of this client with Mox and use it in the tests but for this I need some @behaviour to start from.

Do you know if the Gun client is based on some standard behaviour?

Or must I extract the behaviour of this module?

I know I could use grpc_mock but I’d like to avoid it in this case.

Thanks.

1 Like

It looks like that library doesn’t define a @behaviour for the gun client so you’ll have to derive it yourself. Based on what I see in lib/grpc/stub.ex, you’ll probably need to write callbacks for these functions:

  • connect/2
  • disconnect/1
  • send_request/3
  • send_headers/2
  • end_stream/1
  • cancel/2
  • recv_headers/3
  • recv_data_or_trailers/3

If you write up some nice callbacks for these functions I’m sure the library maintainer(s) would appreciate a PR to add it to the project!

Thanks @the-mikedavis,

I already tried to extract the @behaviour from the module public functions, some of them have @spec's while others don’t, and I have to figure out the correct @spec for those, hence the question :slight_smile:

ah yep you’ll have to infer the types for ones that are missing

I think this is generally considered bad… behaviour (haha) but you can always use the any() type when in doubt:

# test/support/my_grpc_gun_adapter_behaviour.ex
defmodule MyGrpcGunAdapterBehaviour do
  @callback connect(GRPC.Channel.t(), any()) :: {:ok, GRPC.Channel.t()} | {:error, any()}
  @callback disconnect(any()) :: any()
  # ..
end

Mox.defmock(MyGrpcGunAdapterMock, for: MyGrpcGunAdapterBehaviour)

since Mox doesn’t check the types of the callbacks (although hammox does that if you’re interested in that kind of rigor)