I try mock library for my tests. But I cannot mock function with it
This is my test code
test "test" do
with_mock Process, send_after: fn _, _, _ -> :ok end do
assert Process.send_after(self(), :message, 5_000) == :ok
end
end
and I notice that Process module is not mocked. with_mock works well with other modules but it did not work with Process. Why this happens and how can I solve this?
Sorry for confusing you. That’s not my real test code. It’s just sample code to explain my problem. And I can mock modules like String, IO. So I guess it would be work with Process too.
Sorry to keep arguing against mocking – still, your use-case has other ways to be tested.
As a start, you can change the behavior of the cleanup depending on Mix config / environment / config file so you can have immediate message sending when testing. There are a number of ways to do it but let’s just try this one (I admit I started getting lost on how Elixir’s core team prefers we do config lately):
Which means that :dev and :test will send the cleanup message immediately but :prod (and by extension, your release produced by e.g. mix release) will send it after 5 seconds.
Then you can assert on the observable effect of the cleanup i.e. you can send a message to the worker getting the new state and you can ensure that something was deleted from it by a simple assert with map arguments. Or if you are feeling very adventurous and want to redirect messages and change the runtime topology for tests you can also reach for assert_received but that’s little more involved.
I am not going into details because your original problem seems to have a number of possible ways to go about it.
Mocking is quicker and easier in many cases but I’d prefer to actually test the result of the thing I’d be tempted to mock. Checking if Process.send_after was invoked is testing an implementation detail.
Thank you for your response. I learned a lot from reading your message. Although I felt a little uneasy that the logic being tested is not the logic of the prod (although the code has changed a bit), I don’t think it’s a big issue, and I think it’s a good solution. Thank you once again for your reply. I will also try the direction you suggested!
I think it might be difficult to easily change the dependencies since it’s not a personal project, but I will give it a try. Thank you for the suggestion.
You can use Patch alongside any other Mock project. Most of the mocks in Elixir are written in a way of explicit dependency injection, while Patch actually changes the module code in very efficient way.
Ever since I found Patch, I’ve stopped using Mex, Mox, Protomox and other dependency injection tooling
I tried experimenting with the following code, but the test didn’t end. Although the behavior has changed, it didn’t return “:ok” as I wanted it to. The issue is not resolved, but the Patch library itself looks promising.
use Patch
test "test" do
patch(Process, :send_after, fn _, _, _ -> :ok end)
assert :ok == Process.send_after(self(), :hi, 1_000)
end