Hi all!
I’ve been working with Mox for a while now and I developed a small DSL for an application I’m building which allowed me in some way to set up certain mocks to model “the world”. The thing is, currently I started developing a feature which required me to change that “world” mid-test – I needed to set the mocks, do some operations, and then reset the mocks so they would return different things, and finally do some more operations.
When trying to achieve this, I saw that I wasn’t able to… but I’m kind of lost as in to why. On the one hand, a part of me understand that mocks should be deterministic, so I should potentially find a different way of doing this, but on the other hand, I really liked the metaphor of having an API which would allow me to interact with the “world” with some really expressive functions. And I also want to understand what’s happening under the hood.
The code I’m trying to make work looks like the following:
World.new()
|> World.with_disruption("circle", "Severe Delays", "Description about delays")
|> World.create()
StatusBroker.fetch_latest()
World.new()
|> World.with_disruption("northern", "Everything is broken", "oooops")
|> World.create()
diff = StatusBroker.changes()
# assert stuff here
And it’s that World.create/0
function which sets up several mocks like the following:
Application.get_env(:londibot, :subscription_store)
|> expect(:save, @expected_executions, fn _ -> :ok end)
|> expect(:all, @expected_executions, fn -> subscriptions end)
|> expect(:fetch, @expected_executions, fn id -> Enum.find(subscriptions, &(&1.id == id)) end)
Here is the world.ex in case anybody wants more context.
If anybody has any insight as in to why I can’t reset those expects in the mocks, it would be awesome!
Thanks in advance!