[![Contributor Covenant](](

# MockGRPC

Concurrent mocks for [gRPC Elixir](

## Installation

Add `mock_grpc` to your list of dependencies:

def deps do
    {:mock_grpc, "~> 0.1"},

    # You also need to have gRPC Elixir installed
    {:grpc, "~> 0.6"}

## How to use

Imagine that you have a module calling a `say_hello` RPC.

defmodule Demo do
  def say_hello(name) do
    {:ok, channel} = GRPC.Stub.connect("localhost:50051")
    GreetService.Stub.say_hello(channel, %SayHelloRequest{name: "John Doe"})

The first step is to change the `connect` code to use an adapter coming from the app environment, so that you can use `MockGRPC` in test mode, and the default adapter in dev and production.

{:ok, channel} =
    adapter: Application.get_env(:demo, :grpc_adapter)

Or if you're using [`ConnGRPC`](, add `adapter` to the channel `opts`.

Then, on your `config/test.exs`, set it to `MockGRPC.Adapter`:

Application.put_env(:demo, :grpc_adapter, MockGRPC.Adapter)

Now it's time to write your test. To enable mocks, add `use MockGRPC` to your test, and call `MockGRPC.expect/2` or `MockGRPC.expect/3` to set expectations.

defmodule DemoTest do
  use ExUnit.Case, async: true

  use MockGRPC

  test "say_hello/1" do
    MockGRPC.expect(&GreetService.Stub.say_hello/2, fn req ->
      assert %SayHelloRequest{name: "John Doe"} == req
      {:ok, %SayHelloResponse{message: "Hello John Doe"}}

    assert {:ok, %SayHelloResponse{message: "Hello John Doe"}} = Demo.say_hello("John Doe")

For more info, see [`MockGRPC` on Hexdocs](

## Code of Conduct

This project uses Contributor Covenant version 2.1. Check [](/ file for more information.

## License

MockGRPC source code is released under Apache License 2.0.

Check [NOTICE](/NOTICE) and [LICENSE](/LICENSE) files for more information.