Search code examples
databasetestingruby-on-rails-4rakeprepare

Rails 4 auto test schema not working


I have an app that has been upgraded to rails 4.1.8. I have been hacking around trying to make rails 'automatically manage' my test schema...but, no matter what...I have to manually prepare the test database. Im using MiniTest and Fixtures.

Here is my test_helper.rb

  ENV["RAILS_ENV"] = "test"
  require File.expand_path("../../config/environment", __FILE__)

  require "rails/test_help"
  require "minitest/rails/capybara"
  require 'minitest/rg'

  Capybara.default_driver = :selenium

  ActiveRecord::Migration.maintain_test_schema!

  class ActionDispatch::IntegrationTest
    # Make the Capybara DSL available in all integration tests
    include Capybara::DSL
  end

  class ActiveSupport::TestCase
    fixtures :all
    include Sorcery::TestHelpers::Rails
    include Sorcery::TestHelpers::Rails::Controller
    extend MiniTest::Spec::DSL

    # http://blowmage.com/2013/07/08/minitest-spec-rails4
    register_spec_type self do |desc|
      desc < ActiveRecord::Base if desc.is_a? Class
    end

    def sample_file(filename = "test_image.png")
      File.new("#{Rails.root}/test/#{filename}")
    end
  end

The important line is "ActiveRecord::Migration.maintain_test_schema!"...which is supposed to handle the test schema automatically. However, it does not. I regularly pull in my production database. After which...the test db will be gone...so it needs to be re-created.

If I run:

bundle exec rake test

It will fail with:

ActiveRecord::NoDatabaseError [...]

So, the test db was not automatically managed by rails. Because...its not there.

If I run bundle exec rake db:create it will fail because the development database already exists. If I run bundle exec rake db:migrate it will execute but will not affect the test db.

If I run:

bundle exec rake db:test:prepare

It will print:

WARNING: db:test:prepare is deprecated. The Rails test helper now maintains your test schema automatically, see the release notes for details.

test:prepare works...I go on my merry way of testing. But, soon ... test:prepare will be ...poof...gone.

questions

What am I missing? When dumping and pulling in from production...how would rails know how to handle the test db? This is a common occurrence in the world...how is handled? What happens in cases like this when test:prepare is removed in the next rails release?

Thanks


Solution

  • rails db:test:prepare was added back to rails. Im not sure if there is a more rails way to approach what I have outlined above. I submitted an issue to get answers on this and the reply simply stated that db:test:prepare was added back.

    https://github.com/rails/rails/issues/18045