noelHan
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?
Thanks ![]()
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #elixirconf-us
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #security
- #elixirconf-eu
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
That test looks kind of meaningless though, what is your goal with it?
And I’d imagine that mock libraries don’t attempt to mock language API but not 100% sure of it.
noelHan
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.
dimitarvp
Then my comment was just noise, sorry. Still, if you can explain your use-case maybe other people will be more willing to jump in and help.
noelHan
Thank you for comment! I want to test my genserver’s handle_info function. In handle_info, there is logic like this
And I want to make sure
Process.send_afteris called. That is the reason I want to mock Process moduledimitarvp
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):
and
Which means that
:devand:testwill 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
assertwith 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 forassert_receivedbut 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_afterwas invoked is testing an implementation detail.hst337
Use patch
noelHan
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!
noelHan
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.
hst337
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
al2o3cr
I suspect your difficulty is related to this note in the docs for
Process.send_after:Replacing the
Processmodule won’t do much if the call toProcess.send_afterhas already been compiled down to:erlang.send_after.