...

/

Testing Server-rendered HTML Applications

Testing Server-rendered HTML Applications

Learn how to test server-rendered HTML endpoints.

We'll cover the following...

Testing server-rendered HTML endpoints is very similar to testing JSON endpoints, but the response is an HTML document instead of JSON.

Controller

We’ll write tests for the user controller that handles the server-rendered HTML endpoints. Let’s start by looking at the controller code that we’ll be testing:

Press + to interact
#file path -> lib/not_skull_web/controllers/user_controller.ex
#add this code at the indicated place mentioned in comments of lib/not_skull_web/
#controllers/user_controller.ex in the playground widget
defmodule NotSkullWeb.UserController do
use NotSkullWeb, :controller
alias NotSkull.Accounts
alias NotSkull.Accounts.User
alias NotSkull.ExternalServices.Email
def new(conn, _params) do
user = User.create_changeset(%{})
render(conn, "new.html", changeset: user)
end
def create(conn, %{"user" => user_params}) do
case Accounts.create_user(user_params) do
{:ok, user} ->
Email.send_welcome(user)
conn
|> put_session(:user_id, user.id)
|> put_flash(:info, "Your account was created successfully!")
|> redirect(to: Routes.user_path(conn, :show, user))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
end

This code is a controller that, under the hood, calls to the same context module as the JSON controller we’ve already tested. Our testing will focus on the two endpoints that call the new and create controller acti ...

...

Testing New

...