How to check the broadcast coming to the channel from another channel or controller?

I have a driver channel that passes their location to the provider channel.

defmodule MyApp.DriverChannel do
    #...

    def handle_in("transmit", payload, socket) do
      MyApp.Endpoint.broadcast! "vendors:lobby", "track", payload
      {:noreply, socket}
    end
end

defmodule MyApp.DriverChannelTest do

  #....

  test "location broadcasts to vendors:lobby", %{socket: socket} do
    push socket, "transmit", %{"hello" => "all"}
    assert_broadcast "track", %{"hello" => "all"}
    # Want something like this
    # assert_broadcast "vendors:lobby", "track", %{"hello" => "all"}
  end

end

This statement will fail, because it only checks the broadcast with DriverChannel, how to claim the translation made in VendorChannel, I was looking at the source code, it seems that there is no way to transfer channel_nameto assert_broadcast.

[Another note] I also broadcast the broadcast from the controllers, if I know the answer to this question, I can approve these broadcasts too! :)

+4
source share
2 answers

assert_broadcast , MyApp.Endpoint.subscribe/2 assert_receive:

test "location broadcasts to vendors:lobby", %{socket: socket} do
  MyApp.Endpoint.subscribe(self, "vendors:lobby")
  push socket, "transmit", %{"hello" => "all"}
  assert_broadcast "track", %{"hello" => "all"}
  assert_receive %Phoenix.Socket.Broadcast{
    topic: "vendors:lobby",
    event: "track",
    payload: %{"hello" => "all"}}
end
+3

self

  • : MyApp.Endpoint.subscribe("vendors:lobby")
  • : MyApp.Endpoint.unsubscribe("vendors:lobby")

, :

[warning] Passing a Pid to Phoenix.PubSub.subscribe is deprecated. Only the calling process may subscribe to topics

:

test "location broadcasts to vendors:lobby", %{socket: socket} do
  MyApp.Endpoint.subscribe("vendors:lobby")
  push socket, "transmit", %{"hello" => "all"}
  assert_broadcast "track", %{"hello" => "all"}
  assert_receive %Phoenix.Socket.Broadcast{
    topic: "vendors:lobby",
    event: "track",
    payload: %{"hello" => "all"}
  MyApp.Endpoint.unsubscribe("vendors:lobby")
end
+1

Source: https://habr.com/ru/post/1613970/


All Articles