Skip to content

Instantly share code, notes, and snippets.

@sevenseacat
Last active June 7, 2018 02:59
Show Gist options
  • Save sevenseacat/913464ab9f6dff8306ab4b7e2eb90bb2 to your computer and use it in GitHub Desktop.
Save sevenseacat/913464ab9f6dff8306ab4b7e2eb90bb2 to your computer and use it in GitHub Desktop.

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:

  1. 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.
  2. That it assigns that data to the right conn.assigns value
  3. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment