boriscy

boriscy

How can I test a function that has been called

I have this code in my controller

  def create(conn, %{"order" => order_params}) do
    order_params = get_params(conn, order_params)

    case Order.create(order_params) do
      {:ok, order} ->
        Publit.OrganizationChannel.broadcast_order(order)
        render(conn, "show.json", order: order)
      {:error, cs} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render("errors.json", cs: cs)
    end
  end

I want to be able to test that the Publit.OrganizationChannel.broadcast_order(order) method has been called in my controller tests, I have created tests for my channel but have not found a way to pull this out.

Most Liked

OvermindDL1

OvermindDL1

In my opinion, the reason that mock’ing is wrong in the functional world is that a function does not ‘truly’ exist, rather in a purely functional world you can inline every single function in to every single place (in reality you could not, but assume this for now), thus checking that a function is called makes no sense.

In the functional world it is better to test contracts, since a function should always return the same data for the same input if properly made (and enforced in many functional languages, including Erlang/Elixir) you can just test for that, and in fact you can build up test cases dynamically via PropEr and such. Since the EVM has fully segmented ‘units’ called processes that makes it easy to test for messages being sent between them. And with this realization you may also notice that in the OOP world you can only mock ‘objects’, and object in the OOP world is most similar to a process in the EVM world, and you can and indeed ‘should’ mock processes, but do not ever mock functions, modules, or anything else at all, only processes.

Given that:

  • Your functions should be pure (they may send or receive a message, but you can mock those), so testing them becomes trivial.
  • Your integration tests should be pure (they may also send or receive messages, but you can mock those too).
  • And more importantly, you can let the system mostly test itself once you tell it how to via things like PropEr or Quickcheck (does Quickcheck still have that abysmal license?).

Thus by asking ‘How can I test a method has been called’ shows that you are testing the entirely wrong thing. You should be testing that a specific function returns an appropriate output for the given input, or sends an appropriate message, or so forth.

Every module that wraps a process should have some method of being able to override its name or PID (there is always ‘some’ way, though refactoring it a bit can make it substantially easier).

:slight_smile:

scrogson

scrogson

Phoenix Core Team

You can test this much easier than you think. In your test you can simply subscribe the test process to the event that will be broadcasted and use assert_receive…sorry on mobile.

dom

dom

Providing some links:

Something like Publit.PubSub.subscribe(topic) then assert_receive later should be enough, no mock involved.

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement