Hi,
I’m using Mox and I defined a Shortlist
behaviour like this:
defmodule Shortlist do
@type result :: {:ok, Item.t} | {:error, reason}
@callback save(Item.t) :: result
end
As it is now, if I want to implement it with an Agent, that gives me 2 options:
- the implementation would rely on a named Agent process so it won’t require the pid of the Agent in the function signature. Of course, I’ll have to do a bit of test setup to start/stop the process around each test and I won’t be able to run them
async
- or, if I change the function signature, I can receive an extra pid param, allowing tests to run against distinct agents in order to run concurrently
Yet, that is narrowing the possible implementations to only process based implementations. Instead, if I want to rely on an ETS table, or Ecto.Repo, or a remote http service, or…, I don’t believe this behaviour currently allows that.
I think that changing it to a Protocol would give me total freedom… Example:
defprotocol Shortlist do
def save(shortlist, item)
end
defmodule InMemoryShortlist do
defstruct [:agent_pid]
defimpl Shortlist do
def save(in_memory_shortlist, item) do
# send message with item in its payload to in_memory_shortlist.agent_pid
end
end
end
Great, but this comes at the cost of losing Mox to write expectations. Sure, I can make each test provide its own implementation of the protocol (hopefully some will be reused) and use assert_received
. That will be quite a ceremony though.
Have I overlooked something?