Mocking away dependencies — especially external dependencies — is common practice when writing tests. Dependency injection typically makes it easy to provide a mock implementation for your dependencies, e.g. a database.

In this article we will discuss how to remove the need for mocking a SQL database.

Mock implementations almost always return static data which makes the test run whatever paths you think must be tested. It can be time consuming and difficult to predict all possible scenarios. Furthermore, an issue in the SQL query itself will never be caught in a mocked test. Even trivial syntax errors such as SELECT * FORM my_table will slip through.

You might be tempted to use some in-memory database in your tests. They’re fast, but will likely not match the exact SQL dialect you’re using. You will spend time finding quirks to get the queries, seeds and migrations to run in both dialects — even worse, you might be tempted to change your production code or run different code paths in test and production 😱.