Validating fixtures before running the test suite

You don't always have to run a full test to quickly work on your fixture files. Here's how to quickly check they would work.

Table of contents

Loading fixtures data

Rails comes with database fixture commands under db:fixtures namespace and the one we need to check for loading the fixtures without issues is load :

bin/rails db:fixtures:load RAILS_ENV=test

This command will try to load all fixtures into your test database. If there are YAML errors, missing references, or foreign key violations, it will fail right away. If we run this later in development, we get to work with our fixtures state in the application instead of trying to manually add some data.

There is no problem in loading fixtures again and again since that's what's happening with every single test anyways.

But why?

The reason to run this command before running any tests is that you'll know your tests aren't just failing because you introduced a slight error in your fixtures. It's very useful after refactoring lots of fixtures at once.

Author
Josef Strzibny
Hello, I am Josef and I am on Rails since its 2.0 version. I always liked strong conventions and the Rails Omakase docrine. I am author of Kamal Handbook and Test Driving Rails. I think testing should be fun, not a chore.

© Test Driving Rails Blog