noelHan

noelHan

Cannot mock Process module with Mock.with_mocks macro

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:

Marked As Solved

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.

Also Liked

noelHan

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.

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

Last Post!

noelHan

noelHan

Without a doubt, this seems to be the cause of the problem. Thank you. :raised_hands:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New

We're in Beta

About us Mission Statement