For a super quick example, this is the default index controller action test in Phoenix:
describe "index" do
test "lists all products", %{conn: conn} do
conn = get(conn, product_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Products"
end
end
I would want to test three things about this action:
- That it calls my context function to load data (in this case,
Store.load_products
). Ideally I'd stub that out - what it returns is irrelevant. - That it assigns that data to the right
conn.assigns
value - That it renders the right template (
products/index.html.eex
)
Checking the actual HTML response should be the responsibility of dedicated view tests.
template = ProductView.render("index.html", %{products: [%Product{name: "foo"}])
# assert stuff on template contents here