Closed Fl4m3Ph03n1x closed 2 years ago
Thanks for the detailed question!
Unfortunately, what you need to do is not currently possible with Mock and I don't have good ideas of how to achieve it.
It seems like you have a stateful function which kind of breaks the functional paradigm of elixir. With that being said, I do recognize that there are always exceptions and code is never perfect.
If you have an idea of how to implement this, PRs are welcome!
Hello @Olshansk . Everyone needs to manage state somewhere in their applications. If an application does not interact with the outside world or doesn't manage state whatsoever ... well, I find hard to imagine such an application :D
In this specific case, the Storage module is responsible for making CRUD operations in ETS tables. I use Mock to simulate these calls so I don't actually have to spin up an ETS table, populate, etc.
While I try to follow the functional paradigm as much as I can, I admit I have my own limits. If you know any patterns or recommend any books that you think would make this post useless, feel free to let me know.
As for ways of doing this, I am aware of at least 2:
I was not aware of meck.seq
, but that sounds really useful and powerful.
If you were to submit a PR with a light wrapper around it, I'd definitely support it! 💯
Working on a PR. It's a very thin wrapper. But not too hard if we document what needs to be done.
Awesome. Thanks for putting this together @tpitale!
Background
I am working in a project that uses this library for testing. However I need to run a specific scenario, where the output of a function depends on the number of times said function is called. I have read the documentation and I don't think it is possible to do this with Mock, but perhaps I am missing something.
Code
In this test I have a Storage module which I want to mock (it has side effects and is a boundary). In the test, I call a function
get
, which the first time returnsnil
, then I save some data withsave
and then I call get again.The issue here is that since there is no counter, I don't have a way of returning a different output depending on the number of calls a function was called.
To be fair, Mock does offer a way to returns different outputs when the input is different. However, this is not the case. The input is the same, the only thing different is the number of invocations.
I am aware of call history and meck, but as far as I understand, this is used more for asserts and not used to return specific outputs (as in, this is a history stack).
Question
How can I achieve my goal using Mock? (is it possible?)