superruzafa

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

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/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!

Last Post!

the-mikedavis

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)

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement