noelHan

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 :slight_smile:

Showing Posts 1 to 10

dimitarvp

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

noelHan OP

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

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

noelHan OP

Thank you for comment! I want to test my genserver’s handle_info function. In handle_info, there is logic like this

if state.marks != %{} do
  Process.send_after(state.cleaner, {:delete, state}, 5_000)
end

And I want to make sure Process.send_after is called. That is the reason I want to mock Process module

dimitarvp

dimitarvp

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):

# config/config.exs

config :YOUR_APP_HERE, :state_cleaner_fn,
  fn(worker) -> Process.send(worker, {:delete, state}) end

and

# config/prod.exs

config :YOUR_APP_HERE, :state_cleaner_fn,
  fn(worker) -> Process.send_after(worker, {:delete, state}, 5_000) end

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.

hst337

hst337

Use patch

noelHan

noelHan OP

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

noelHan OP

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

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

al2o3cr

I suspect your difficulty is related to this note in the docs for Process.send_after:

Inlined by the compiler.

Replacing the Process module won’t do much if the call to Process.send_after has already been compiled down to :erlang.send_after.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews