简体   繁体   English

如何在 Phoenix LiveView 中测试 handle_info/2?

[英]How to test handle_info/2 in Phoenix LiveView?

Greetings Phoenix LiveView Wizards!问候 Phoenix LiveView 向导! 👋 👋

Context语境

We have a basic LiveView counter app: https://github.com/dwyl/phoenix-liveview-counter-tutorial我们有一个基本的 LiveView 计数器应用程序: https : //github.com/dwyl/phoenix-liveview-counter-tutorial
The code is very simple: /live/counter.ex代码很简单: /live/counter.ex
The App works as expected, see: https://live-view-counter.herokuapp.com该应用程序按预期工作,请参阅: https : //live-view-counter.herokuapp.com

The test file is: test/live_view_counter_web/live/counter_test.exs测试文件为: test/live_view_counter_web/live/counter_test.exs
We are stuck with trying to invoke the handle_info/2 function in a test.我们一直在尝试在测试中调用handle_info/2函数。
So we have code in our project that is untested.所以我们的项目中有未经测试的代码。 Which is undesirable.这是不可取的。
See: https://codecov.io/gh/dwyl/phoenix-liveview-counter-tutorial/src/master/lib/live_view_counter_web/live/counter.ex参见: https : //codecov.io/gh/dwyl/phoenix-liveview-counter-tutorial/src/master/lib/live_view_counter_web/live/counter.ex

反不包括

We have read through the official docs https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html我们已经阅读了官方文档https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html
but have not been able to understand how to do it.但一直无法理解如何去做。 What are we missing?我们缺少什么?

We really want to use LiveView in our "real" projects, but we want to ensure that our LiveView apps are fully tested.我们真的想在我们的“真实”项目中使用LiveView ,但我们希望确保我们的LiveView应用程序经过全面测试。

Question

How do we write a test to invoke the handle_info/2 function?我们如何编写测试来调用handle_info/2函数?

After much research, trial and error, error, error (iteration), we came up with the following test:经过大量研究,反复试验,错误,错误(迭代),我们提出了以下测试:

test "handle_info/2", %{conn: conn} do
  {:ok, view, disconnected_html} = live(conn, "/")
  assert disconnected_html =~ "Count: 0"
  assert render(view) =~ "Count: 0"
  send(view.pid, %{payload: %{ val: 1 }})
  assert render(view) =~ "Count: 1"
end

Thanks to @daniel for pointing us in the direction of the send/2 function.感谢@daniel 为我们指明了send/2函数的方向。
and @AlekseiMatiushkin for patiently asking probing questions above.和@AlekseiMatiushkin 耐心地提出上述探索性问题。 👍 Thanks to @chrismccord for the insight: https://elixirforum.com/t/how-to-test-handle-info-2-in-phoenix-liveview/30070/7 👍 感谢@chrismccord 的洞察力: https ://elixirforum.com/t/how-to-test-handle-info-2-in-phoenix-liveview/30070/7

handle_info/2 is a general behavior of Genserver . handle_info/2Genserver的一般行为。 If you read documentation, you can find:如果您阅读文档,您可以找到:

Besides the synchronous and asynchronous communication provided by call/3 and cast/2 , "regular" messages sent by functions such as Kernel.send/2 , Process.send_after/4 and similar, can be handled inside the handle_info/2 callback.除了call/3cast/2提供的同步和异步通信之外, Kernel.send/2Process.send_after/4等函数发送的“常规”消息可以在handle_info/2回调中处理。

So you can send either of those as long as you know the pid of the process.因此,只要您知道进程的pid ,您就可以发送其中任何一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM