ream88

ream88

Forwarding a match pattern to a custom macro

Hey,

I’m currently trying to DRY up some tests which have a lot of boilerplate code in the form of:

test "user is updated", %{user_id: user_id} do
  pid = spawn(fn ->
    EventStore.subscribe(UserUpdated)
    assert_receive event = %UserUpdated{aggregate_id: ^user_id}
    EventStore.acknowledge(event)
  end)

  ref = Process.monitor(pid)

  # the actual test

   assert_receive {:DOWN, ^ref, _, _, _}
end

Basically I’m checking if the event UserUpdated is fired in our custom event_store. I tried to implement a custom macro like this:

defmacro assert_event(event, do: block) do
  quote do
    pid =
      spawn(fn ->
        EventStore.subscribe(unquote(event).__struct__)
        assert_receive event = unquote(event)
        EventStore.acknowledge(event)
      end)

    ref = Process.monitor(pid)

    unquote(block)

    assert_receive {:DOWN, ^ref, _, _, _}
  end
end

Which can then be used like this:

test "user is updated", %{user_id: user_id} do
  assert_event %UserUpdated{aggregate_id: user_id} do
    # the actual test
  end
end

But this results in the following warning:

warning: variable “user_id” is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)

But pinning user_id results in the following error:

cannot use ^user_id outside of match clauses

So, my question is (after reading the complicated source code of assert_receive), is there a way to mark some code as a pattern and just forward it to the assert_receive macro inside my custom assert_event macro?

Marked As Solved

LostKobrakai

LostKobrakai

Pattern matches are a compile time structure in code. You cannot „pass“ a pattern at runtime, so the only way to do it is using macros. When using macros you‘ll need to read up on macro hygine, so variables within generated code become accessible to code not being generated.

Also Liked

stefanchrobot

stefanchrobot

Why not keep it simple and use a function?

def assert_event(%event_mod{} = event, func) do
  pid = spawn(fn ->
    EventStore.subscribe(event_mod)
    assert_receive ^event
    EventStore.acknowledge(event)
  end)

  ref = Process.monitor(pid)

  func.()

  assert_receive {:DOWN, ^ref, _, _, _}
end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
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
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement