Testing Contexts

It’s time to test the M of the MVC, models. Phoenix generates a module in test/support/data_case.ex to serve as a foundation for your tests that interact with the database. In our case, Accounts and Multimedia contexts both work with the database. The data_case handles setup and teardown of the database and integrates with Ecto.Sandbox to allow concurrent transactional tests. Crack it open and import the fixtures we just defined:

1: defmodule​ Rumbl.DataCase ​do
use​ ExUnit.CaseTemplate
using ​do
5: quote​ ​do
alias Rumbl.Repo
import​ Ecto
import​ Ecto.Changeset
10: import​ Ecto.Query
import​ Rumbl.DataCase
import​ Rumbl.TestHelpers ...

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