Testing Views and Templates

As we’ve said, any code worth writing is code worth testing, and your views are no exception. As you saw in Chapter 3, Controllers, Views, and Templates, Phoenix templates are simply functions in a parent’s view module. You can test these functions like any other. In this section, you’ll see how to test views and templates in isolation.

Create a test/views/video_view_test.exs and key this in:

1: defmodule​ Rumbl.VideoViewTest ​do
use​ Rumbl.ConnCase, ​async:​ true
import​ Phoenix.View
5:  test ​"​​renders index.html"​, %{​conn:​ conn} ​do
videos = [%Rumbl.Video{​id:​ ​"​​1"​, ​title:​ ​"​​dogs"​},
%Rumbl.Video{​id:​ ​"​​2"​, ​title:​ ​ ...

Get Programming 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.