ExUnit assert_receive specify module name in message?

I’m writing a test to make sure the correct version of function is called. I’ve got:

defmodule Foo do 
  def query do 
  end
end

and

defmodule Bar do
  def query do 
  end
end

and finally

defmodule Test do 
  def run do 
    case something do 
      true -> Bar.query()
      _ -> Foo.query()
    end
  end
end

In my test I’m doing:

test "something" do 
  Test.run()
  assert_receive :run
end

This fails with

“No message matching :run after 100ms.
The process mailbox is empty.”

Is there a way using ExUnit assert_receive(d) to decide which one was called when I call Test.run() ? I might be just thinking about this the entirely wrong way, I guess.

You can send module name from both implementations:

def query do
  send self(), __MODULE__
end

And distinguish them in tests:

assert_receive Foo
assert_receive Bar

But what do you really want to do?

I’ve vastly simplified the code for the example, and in my real test I’m trying to make sure that when I call the function being tested, it in turn calls the correct function based on input.

Well, without any details it sounds like testing implementation not behaviour.

Jose described several useful techniques in the following article. I hope this could be helpful for you.

1 Like

cool, thank you for your help :slight_smile: