DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Automating Database Seeding for Reliable Tests in Rails

March 19, 2025

When developing a Rails application that involves structured data, testing is crucial to ensure that our data handling works correctly. Recently, I faced a challenge when testing a questionnaire system where I needed to run database seeds before executing test cases. Here’s how I optimized the process.


Need to Improve Your Test Suite?

If you’re looking to enhance the test suite for your website, don’t hesitate to contact me.

Contact Me


Understanding the Schema

My application includes the following models:

  • question_report_versions: Stores versioning information.
  • questions: Holds the questionnaire items.
  • reports and report_users: Manage report entries.
  • answers: Stores user responses linked to questions.

Since questions are part of the Line model, I needed to seed the database before testing answer imports.

Initial Challenges

The primary issues I encountered:

  1. Ensuring the seeds run before the test suite starts.
  2. Keeping tests isolated so data does not persist across runs.
  3. Avoiding redundant seed execution in individual test files.

Optimized Solution

To automate database seeding before tests, I integrated DatabaseCleaner and configured rails_helper.rb efficiently.

1. Install DatabaseCleaner

group :test do
    gem "database_cleaner-active_record"
end 
Enter fullscreen mode Exit fullscreen mode

Run bundle install to ensure the gem is available.

2. Configure rails_helper.rb

require 'database_cleaner/active_record'

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation) # Clears database before tests
    Rails.application.load_seed # Load seeds before test suite
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end 
Enter fullscreen mode Exit fullscreen mode

Why this works:✅ Runs seeds once before the test suite. ✅ Ensures a fresh database before tests. ✅ Prevents test data from interfering with each other.

3. Write Tests to Verify Seeds

Here’s how I verified that the seeded data was correctly loaded:

require 'rails_helper'

RSpec.describe Answer, type: :model do
  it "verifies that seeded questions exist" do
    expect(Question.count).to be > 0
  end

  it "verifies that a seeded question has correct attributes" do
    question = Question.find_by(number: "Q1")
    expect(question).not_to be_nil
    expect(question.body).to eq("What is your name?")
  end
end 
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

With this approach, I successfully automated database seeding while keeping test cases reliable. If you’re working with structured data in Rails, this method can streamline your testing workflow and improve consistency.

How do you handle database seeds in your tests? Let’s discuss in the comments!

Image of Quadratic

Create interactive charts in seconds

Bring your own data or leverage historical data for fast Python-powered data visualizations in a familiar spreadsheet interface.

Try Quadratic free

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay