Write Tests for a LiveView

LiveView, like Channels, provides test helpers that allow you to quickly write meaningful tests for your LiveView module. The Phoenix.LiveViewTest[67] module provides all the helpers you need to get started writing tests.

Our LiveView is fairly simple because it doesn’t respond to front-end events. Our tests will be basic because of this. We’ll ensure that ProductLiveView mounts, renders the correct HTML, updates when shoes release, and updates when shoes sell out.

To get started, we’ll need to add a test dependency to our mix.exs file. LiveViewTest uses Floki to parse HTML, so let’s add that.

 {​:hound​, ​"​​~> 1.0"​},
 {​:floki​, ​"​​>= 0.0.0"​, ​only:​ ​:test​}

As always, run mix deps.get ...

Get Real-Time Phoenix now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.