Why do I need to migrate the test database in Rails?

It might be because of two reasons.

  1. You may have missed it to configure in config/environments/test.rb

Add config.active_record.maintain_test_schema = true if you don't have it or set it to true if you had set it to false.

From the docs

config.active_record.maintain_test_schema is a boolean value which controls whether Active Record should try to keep your test database schema up-to-date with db/schema.rb (or db/structure.sql) when you run your tests. The default is true.

  1. You might have pending migrations after the schema has loaded

From the rspec docs

What this does is that rather than just raising when the test schema has pending migrations, Rails will try to load the schema. An exception will now only be raised if there are pending migrations afterwards the schema has been loaded.

Check whether you have pending migrations with rake db:migrate:status

Also, If you are using SQLite 3.7.9, you should take a look at this discussion


On running your tests, the configurations are loaded in following order (unless you have customized the order of autoload_paths in your rails app):

  1. config/application.rb
  2. config/environments/test.rb
  3. spec/rails_helper.rb

So, the migration pending error you are receiving must be due to config.active_record.migration_error = true this configuration setup somewhere before on the rails engine loads rails_helper.rb where ActiveRecord::Migration.maintain_test_schema! directive is defined.

Try setting config.active_record.migration_error = false on your config/environments/test.rb to skip migration check as it is described in rspec upgrade guide.