Search⌘ K
AI Features

Creating a SchemaCase for Shared Test Code

Explore how to create a SchemaCase template to centralize shared test functions like valid_params and invalid_params for Ecto schemas. Understand modifying mix.exs to include test files during compilation and refactor tests to use the new case template, ensuring maintainable and reusable test code.

We'll cover the following...

Case template

Case templates are handy when identifying a pattern for test files that would benefit from shared code. Our refactors on the basic schema tests created two helper functions, valid_params/1 and invalid_params/1, that would be good to move into a common place to be reused by all our schema tests.

We’ll create a case template called SchemaCase and move the helper functions there. Let’s start by creating a new file called testing_ecto/test/schema_case.ex and add the basic structure for a case ...

Elixir
#file path -> testing_ecto/test/schema_case.ex
#add this code at the indicated place mentioned in comments of testing_ecto/test/schema_case.ex
#in the playground widget
use ExUnit.CaseTemplate
using do
quote do
alias Ecto.Changeset
import TestingEcto.SchemaCase
end
end

For now, ...