superruzafa
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.
First Post!
the-mikedavis
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/2disconnect/1send_request/3send_headers/2end_stream/1cancel/2recv_headers/3recv_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!
Last Post!
the-mikedavis
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)
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










