Creating a Factory to Help with Setup

In testing, we create factories to give ourselves consistent, repeatable ways to provide data for our setup phase. One of the areas where this kind of tooling is the most useful is when interacting with the database.

When we write tests that interact with database tables, we need two sets of data that are either valid or invalid with regard to the table’s definition. As our applications grow larger, we’ll find that having a single source of setup data helps us keep the test suite maintainable. If we update our schema definition, that single source is often the only place (aside from the tests for the schema file itself) that’ll need to be updated in your test suite.

It’s totally possible to write a factory ...

Get Testing Elixir 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.