Test Migrations are not running with my Rails Engine, even though `maintain_test_schema!` is specified

Viewed 2322

I'm developing a Rails Engine (Plugin). So far I have

  • Set up RSpec as a framework
  • Added a migration to create a model/table called MyJob
  • Added some basic model tests under spec/models/my_job_spec.rb

The rails template automatically creates a spec/dummy app, so I did the following to run my test

# Create the development and test DBs
rake db:create

# Copy migrations over to my dummy app
cd spec/dummy
rake my_app:install:migrations
cd ../..

# Run specs
rspec spec/models/my_job_spec.rb

However when I run my specs, I get an error:

> rspec spec/models/
/Users/jeeves/.rvm/gems/ruby-2.2.2@gb/gems/activerecord-5.1.0/lib/active_record/migration.rb:576:in `check_pending!':  (ActiveRecord::PendingMigrationError)

Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=test

I thought this automatically would happen because in my rails_helper.rb I definitely have the following, which is supposed to maintain my test schema for me

ActiveRecord::Migration.maintain_test_schema!

Does it work differently with plugins or engines?

EDIT: I tried the recommendation of running bin/rails db:migrate RAILS_ENV=test, inside spec/dummy/ and then re-running rspec spec/. Still no luck.

3 Answers

I faced similar phenomenon on my engine with MiniTest (not rspec though), which was developed at rails 3, happened when migrating it to rails 4.2.

What I do is that:

  1. I generate new engine under some work directory(e.g. /tmp).
  2. copy newly created test/test_helper.rb to my current engine.

remove ActiveRecord::Migration.maintain_test_schema!

replaced with ActiveRecord::Migrator.migrate(Rails.root.join('db/migrate'))

Related