# Assert\_broadcast unexpected behaviour

**URL:** <https://forum.elixirforum.com/t/assert-broadcast-unexpected-behaviour/35188>\
**Category:** Questions\
**Tags:** channels, phoenix, test\
**Created:** [October 28, 2020, 11:33am UTC](https://forum.elixirforum.com/t/assert-broadcast-unexpected-behaviour/35188 "2020-10-28T11:33:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![francesco.pessina](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/francesco.pessina/32/22814_2.png) [@francesco.pessina](https://forum.elixirforum.com/u/francesco.pessina)\
**Post date:** [October 28, 2020, 11:33am UTC](https://forum.elixirforum.com/t/assert-broadcast-unexpected-behaviour/35188/1 "2020-10-28T11:33:21Z")

</div>

Hello everyone,  
I’m moving my first steps with Elixir/Phoenix, it’s great and also the community is very wide, congrats!  
I’m writing my first post in this forum because I’ve encoutered a strange behaviour and didn’t found any explanation in doc.

While testing channels, when I assert if a message has been broadcasted to the channels (among which there is the channel I subscribed to), all is fine when in `assert_broadcast` macro I pass, for example, a map as payload (example taken from doc):

`assert_broadcast "some_event", %{"data" => "foo"}`

But if I pass a variable as payload, the test always pass also if the variable is different to the expected payload! For example:

```elixir
var1 = %{"data" => "foo"}
var2 = %{"data" => "bar"}

# This passes
assert_broadcast "some_event", %{"data" => "foo"}

# This also passes
assert_broadcast "some_event", var1

# But this also passes, and it shouldn't!
assert_broadcast "some_event", var2

```

Why this?

---

<div class="post-metadata">

**Author:** ![fuelen](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fuelen/32/9870_2.png) [@fuelen](https://forum.elixirforum.com/u/fuelen)\
**Post date:** [October 28, 2020, 12:33pm UTC](https://forum.elixirforum.com/t/assert-broadcast-unexpected-behaviour/35188/2 "2020-10-28T12:33:40Z")

</div>

> [@francesco.pessina](#):
>
> Why this?

because `assert_broadcast` matches by pattern. For your use case you have to use `^` [pin operator](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#%5E/1)

```elixir
assert_broadcast "some_event", ^var1
assert_broadcast "some_event", ^var2

```

otherwise, the value of sent message will be assigned to `var1` or `var2`

---

<div class="post-metadata">

**Author:** ![francesco.pessina](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/francesco.pessina/32/22814_2.png) [@francesco.pessina](https://forum.elixirforum.com/u/francesco.pessina)\
**Post date:** [October 28, 2020, 2:23pm UTC](https://forum.elixirforum.com/t/assert-broadcast-unexpected-behaviour/35188/3 "2020-10-28T14:23:16Z")

</div>

Clear! It was so simple, but I have still to understand well how `^` works 🙂

Thanks a lot!
