silverdr
Phoenix - test whether particular template was rendered / passed to render
Is there an easy method of testing whether a particular template was rendered / passed to render. The guides I read all say about matching output strings as testing method for views/templates. I am looking for a way to test if the expected template is rendered, without taking into account or even knowing what its content is. Suggestions appreciated.
Marked As Solved
kuroda
How about using Phoenix.Controller.view_template/1?
defmodule MyAppWeb.PageControllerTest do
use MyAppWeb.ConnCase, async: true
test "index.html.eex was rendeded", %{conn: conn} do
conn = get(conn, "/")
assert Phoenix.Controller.view_template(conn) == "index.html"
end
end
Also Liked
derek-zhou
Then you can abstract out the internal rules/conditions into a separate module and write unit test on it, right? If you don’t care about the template content why do you want to get the rendering involved in the unit test in the first place?
silverdr
Seems to work well, especially when combined with Phoenix.Controller.view_module(conn), which removes potential ambiguity with template naming.
hauleth
Why testing such implementation? I always test only results, as it doesn’t matter how that result was achieved internally.







