Elixir 如何阅读新凤凰频道实例';信道测试用例中的状态?
我有以下phoenix频道,它处理传入消息,广播消息,然后更新频道实例的Elixir 如何阅读新凤凰频道实例';信道测试用例中的状态?,elixir,integration-testing,phoenix-framework,phoenix-channels,ex-unit,Elixir,Integration Testing,Phoenix Framework,Phoenix Channels,Ex Unit,我有以下phoenix频道,它处理传入消息,广播消息,然后更新频道实例的socket状态: defmodule MyApp.MyChannel do use MyApp.Web, :channel def join("topic", _payload, socket) do {:ok, socket} end def handle_in("update", %{"new_number" => number_}, socket) do broadcast
socket
状态:
defmodule MyApp.MyChannel do
use MyApp.Web, :channel
def join("topic", _payload, socket) do
{:ok, socket}
end
def handle_in("update", %{"new_number" => number_}, socket) do
broadcast socket, "update", %{"new_number" => number_}
{:noreply, assign(socket, :current_number, number_)}
end
...
end
我正试图通过以下测试用例来测试(“更新”,…)函数中的句柄的行为:
test "should broadcast new number and update the relevant instance's socket state", %{socket: socket} do
push socket, "update", %{"new_number" => 356}
assert_broadcast "update", %{"new_number" => 356}
## This is testing against the old state
## which is going to obviously fail
assert socket.assigns[:current_number] == 356
end
:sys.get_state(socket.channel_pid).assigns[:current_number]
这里的问题是,我找不到在测试用例中获取新更新的socket
状态的方法
- Phoenix.ChannelTest
模块中没有assert\u socket\u state
函数,我找不到任何允许获取最新套接字状态的函数
handle\u调用
或handle\u info
,但这意味着我必须获得通道的pid才能调用它们句柄,但我不想在我的频道中加入一个将在生产中可用的内省工具
推送消息后,如何在测试用例中获取更新的
套接字?套接字状态包含一个通道pid
条目,该条目基本上包含通道的pid
前一个与函数相结合,该函数接受GenServer的pid
,并返回其最新的状态
,这是关键
例如,给定测试用例中的socket
状态:
test "should broadcast new number and update the relevant instance's socket state", %{socket: socket} do
push socket, "update", %{"new_number" => 356}
assert_broadcast "update", %{"new_number" => 356}
## This is testing against the old state
## which is going to obviously fail
assert socket.assigns[:current_number] == 356
end
:sys.get_state(socket.channel_pid).assigns[:current_number]
这要归功于他们对这个问题的评论。这是否有效:断言:sys.get\u state(socket.channel\u pid)[:当前\u编号]==356
?