Sandbox Mode

Get introduced to the Ecto Sandbox and how to set it up.

The problem with persistent data

Our database keeps state. We managed to avoid issues with our test on the unique constraint by using randomized data. But even with that, every successive test run leaves one new row in the database.

  • In the short run, it’s doubtful to create a problem because we’re using a new, random string every time we run the test.
  • In the long run, having persistent data between test runs increases the likelihood of having test failures due to unforeseen data collisions.

If the same random string were picked in different test runs, the test would fail on the first inserted user, which was supposed to be part of the setup.

Note: Working against your database in tests is one of the most common places where leftover state changes can cause problems.

Each test works best when operating in a known good environment because that’s the only way to guarantee that their setup is complete and reliable.

Get hands-on with 1200+ tech skills courses.