11

I'm developing a Rails application with Rspec for unit testing.

Weeks ago, Rspec used to migrate the database to the last version automatically when executing 'rake spec', but now it doesn't do it automatically, I have to implement everything for myself.

This happens in test environment, because my development data doesn't desappear.

Is my fault? I didn't change anything, I think :)

2
  • are you getting any errors when you try and execute "rake spec"?
    – StevenMcD
    Commented May 24, 2010 at 17:55
  • If I generate migrations, a "You have pending migrations" raises. At this point I have to migrate the database by myself, and rerun "rake spec"
    – pablorc
    Commented May 25, 2010 at 7:29

6 Answers 6

22

Typically what I do is use an alias command that runs both migrate and prepares the test database.

rake db:migrate && rake db:test:prepare

In your .bashrc just create an alias command like so and then run migrate_databases whenever you need to.

alias migrate_databases='rake db:migrate && rake db:test:prepare'
1
  • I prefer solve the problem instead of hide it, but thanks. BTW, sorry for answer a month later :)
    – pablorc
    Commented Jul 28, 2010 at 8:51
11

My solution for Rails 4:

in spec/spec_helper.rb or anywhere in testing environment initialization code:

# Automigrate if needs migration
if ActiveRecord::Migrator.needs_migration?
  ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
end

UPD: As Dorian kindly pointed out in comments, you don't need to check separately if there are any pending migrations, because ActiveRecord::Migrator.migrate already does this behind the scenes. So you can effectively use just this one line:

ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate'))
2
  • the db:test:clone has deprecated in Rails 4.1
    – Francis.TM
    Commented May 20, 2014 at 6:50
  • You do two SQL requests instead of just one by running ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db/migrate')) for the behavior and a shorter code.
    – Dorian
    Commented Oct 2, 2014 at 19:08
8

Rails 4.1 forward you can use:

ActiveRecord::Migration.maintain_test_schema!

Add at the top of your spec_helper.rb or rails_helper.rb and you're good to go. More info here.

3
  • Can you give me some detail as to how it doesn't work? Does it throw an error of some sort? You may need to make sure your migrations are run in dev mode as well in order for it to work.
    – lobati
    Commented Jan 30, 2016 at 17:36
  • It throws error when I create a new migration and then run rspec Commented Jan 30, 2016 at 19:01
  • You ran the migration first with rake db:migrate?
    – lobati
    Commented Jan 30, 2016 at 19:05
1

Here's my workaround:

Rakefile:

require File.expand_path('../config/application', __FILE__)
require 'rake'
require "rspec/core/rake_task"

MyApp::Application.load_tasks

desc "Run specs"
RSpec::Core::RakeTask.new(:spec)

task :run_specs => ['db:test:clone', :spec] do

end

task :default => :run_specs

Then I run $ rake run_specs

for some reason default task doesn't default to run_specs

0

See if you have the following in your spec_helper.rb? Everytime you run specs, RSpec checks if there are pending migrations.

#Checks for pending migrations before tests are run.
#If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
0

This works even when Rails is not loaded and only does one SQL query most of the time.

if defined?(ActiveRecord::Migrator)
  ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.