grpc_mock v0.3.0 GrpcMock View Source

GrpcMock is library for easy gRPC server mocking to be used with grpc-elixir library.

Concurrency

Unlike mox, GrpcMock is not thread-safe and cannot be used in concurrent tests.

Example

As an example, imagine that your application is using a remote calculator, with API defined in .proto file like this:

service Calculator {
  rpc Add(AddRequest) returns (AddResponse);
  rpc Mult(MultRequest) returns (MultResponse);
}

If you want to mock the calculator gRPC calls during tests, the first step is to define the mock, usually in your test_helper.exs:

GrpcMock.defmock(CalcMock, for: Calculator)

Now in your tests, you can define expectations and verify them:

use ExUnit.Case

test "invokes add and mult" do
  # Start the gRPC server
  Server.start(CalcMock, 50_051)

  # Connect to the serrver
  {:ok, channel} = GRPC.Stub.connect("localhost:50051")

  CalcMock
  |> GrpcMock.expect(:add, fn req, _ -> AddResponse.new(sum: req.x + req.y) end)
  |> GrpcMock.expect(:mult, fn req, _ -> AddResponse.new(sum: req.x * req.y) end)

  request = AddRequest.new(x: 2, y: 3)
  assert {:ok, reply} = channel |> Stub.add(request)
  assert reply.sum == 5

  request = MultRequest.new(x: 2, y: 3)
  assert {:ok, reply} = channel |> Stub.mult(request)
  assert reply.sum == 6

  GrpcMock.verify!(CalcMock)
end

Link to this section Summary

Functions

Define mock in runtime based on specificatin on pb.ex file

Expect the name operation to be called n times

Simmilar to expect/4 but there can be only one stubbed function. Number of expected invocations cannot be defined

Verify that all operations for the specified mock are called expected number of times and remove all expectations for it

Link to this section Functions

Define mock in runtime based on specificatin on pb.ex file

Example

GrpcMock.defmock(CalcMock, for: Calculator)
Link to this function expect(mock, name, n \\ 1, code_or_value) View Source

Expect the name operation to be called n times.

Examples

If code_or_value is a function, it will be invoked as stub body. To expect add to be called five times:

expect(MyMock, :add, 5, fn request, stream -> ... end)

If code_or_value is anything other than a function, it will be stub return value. To expect add to be called once:

expect(CalcMock, :add, AddResponse.new(sum: 12) end)

expect/4 can be invoked multiple times for the same name, allowing different behaviours on each invocation.

Link to this function stub(mock, name, code_or_value) View Source

Simmilar to expect/4 but there can be only one stubbed function. Number of expected invocations cannot be defined.

Example

If code_or_value is a function, it will be invoked as stub body.

stub(CalcMock, :add, fn(request, _) -> ... end)

If code_or_value is anything other than a function, it will be stub return value.

stub(CalcMock, :add, AddResponse.new(sum: 12) end)

Verify that all operations for the specified mock are called expected number of times and remove all expectations for it.