Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 698
user2490003 Avatar asked May 29 '17 00:05

user2490003


2 Answers

Ok found a few things I was doing wrong for anyone else who stumbles across this.

Firstly, I don't need to run rake my_app:installations:migrations while testing or developing. Apparently that's intended only for the downstream users of my app when they want to copy my engine's migrations into their host app. The instructions in various blogs and such mention that in retrospect, but I still think it's easy to misinterpret if it's your first time trying to figure it out.

Secondly, all commands are just to be executed from your engine's root and rails ensures that they are applied correctly. So it's just a matter of running rake db:migrate from your engine root and the dummy application will be migrated.

Thirdly, I stopped using ActiveRecord::Migration.maintain_test_schema!. As the user above noted, it doesn't really work as advertised. rake db:migrate RAILS_ENV=test correctly migrated my test db but because of the way the nested dummy app is set up that the maintain_test_schema! method things nothing has been migrated. It's kind of annoying to not have it automatically migrated in both environments, but I'll take it.

So the whole process, quite simple in the end, boils down to:

# Create the development and test DBs
rake db:create

# Migrate
rake db:migrate
rake db:migrate RAILS_ENV=test

# Run specs
rspec spec/models/my_job_spec.rb

Again, seems simple in retrospect but learned a few things on the way.

like image 78
user2490003 Avatar answered Oct 01 '22 20:10

user2490003


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.
like image 41
Fumisky Wells Avatar answered Oct 01 '22 20:10

Fumisky Wells