jjh42 / mock

Mocking library for Elixir language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: support for `on_exit` style teardown with `setup_with_mocks`

Bastes opened this issue · comments

Hi :) awesome lib you've got there...

...but there's something that could make it awesome-er!

See, the setup_with_mocks macro is great, but it does not let us execute an on_exit instruction during the mocking, but after it, which in turn makes it awkward to do something like that:

setup_with_mocks([
  {MyModule, [:passthrough], [:index, fn -> "fake_index" end]}
]) do
  on_exit(fn -> IO.inspect(MyModule.index(), label: "expects fake_index")
end

The use case I've come across is my app works with Elasticsearch, and we've dockerized an ES instance that's shared by both the dev and test instances. In order to not destroy the dev index(es) every time the tests run, yet really test real requests, we want to insert data before each test, mocking the index name so we don't use the same as the dev, then destroy the index aterwards.

So, we're just going to destroy the index before the tests run for now (plus it allows us to insure our instance is clean before each tests) but maybe we're not the only ones that need to tear down stuff from time to time, so here's that ;)

If I find how to do it, I'll be sure to post a PR.