为什么我需要在Rails中迁移测试数据库?

时间:2017-06-07 22:34:44

标签: ruby-on-rails rails-migrations

创建新的迁移文件后,运行迁移,然后运行我收到的测试:

Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::PendingMigrationError:

  Migrations are pending. To resolve this issue, run:

          bin/rails db:migrate RAILS_ENV=test

rails_helper.rb中的以下代码段是否应该将迁移应用到测试数据库?

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

更新

这是我要求的config/environments/test.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # The test environment is used exclusively to run your application's
  # test suite. You never need to work with it otherwise. Remember that
  # your test database is "scratch space" for the test suite and is wiped
  # and recreated between test runs. Don't rely on the data there!
  config.cache_classes = true

  # Do not eager load code on boot. This avoids loading your whole application
  # just for the purpose of running a single test. If you are using a tool that
  # preloads Rails for running tests, you may have to set it to true.
  config.eager_load = false

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true
  config.public_file_server.headers = {
    'Cache-Control' => 'public, max-age=3600'
  }

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Raise exceptions instead of rendering exception templates.
  config.action_dispatch.show_exceptions = false

  # Disable request forgery protection in test environment.
  config.action_controller.allow_forgery_protection = false
  config.action_mailer.perform_caching = false

  # Tell Action Mailer not to deliver emails to the real world.
  # The :test delivery method accumulates sent emails in the
  # ActionMailer::Base.deliveries array.
  config.action_mailer.delivery_method = :test

  # Print deprecation notices to the stderr.
  config.active_support.deprecation = :stderr

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

3 个答案:

答案 0 :(得分:4)

在运行测试时,配置按以下顺序加载(除非您在rails应用程序中自定义了autoload_paths的顺序):

  1. config / application.rb
  2. 配置/环境/ test.rb
  3. 规格/ rails_helper.rb
  4. 因此,您收到的迁移暂挂错误必须归因于config.active_record.migration_error = true此配置设置在rails引擎加载rails_helper.rb之前某处,其中ActiveRecord::Migration.maintain_test_schema!指令已定义。

    尝试在 config / environments / test.rb 上设置config.active_record.migration_error = false以跳过迁移检查。

答案 1 :(得分:1)

可能是因为两个原因。

  1. 您可能错过了在config/environments/test.rb
  2. 中配置的内容

    如果您没有config.active_record.maintain_test_schema = true,请添加true,如果您将其设置为false,则将其设置为rake db:migrate:status

    来自docs

      

    config.active_record.maintain_test_schema是一个布尔值   控制Active Record是否应该尝试保留您的测试数据库   运行时,架构与db / schema.rb(或db / structure.sql)保持同步   你的考试。默认值为true。

    1. 架构加载后,您可能有 待定迁移
    2. 来自rspec docs

        

      这样做的目的不仅仅是提高测试架构   有挂起的迁移,Rails将尝试加载架构。 <强>一种   现在只有在有待迁移的情况下才会引发异常   之后架构已加载。

      检查您是否有var myApp = angular.module('myApp', []); //myApp.directive('myDirective', function() {}); //myApp.factory('myService', function() {}); function MyCtrl($scope) { $scope.name = 'Superhero'; $scope.data = { "AnomalyEnableLatch": "false", "DistLogPeriod": "0", "AedCooldown": "0", "AedEnableLatch": "false", "AedGuardBand": "0", "AedSensitivity": "0", "AedWindowSize": "0", "AnomalyCurrentNoiseFloor": "10", "AnomalyGuardBandSize": "32", "AnomalyKsigToStart": "40", "AnomalyMinSnrToStop": "100", "AnomalyWindowSize": "651" }; $scope.keys = Object.keys($scope.data); $scope.keys.sort(function(a, b) { return getDifference(a, b, "Aed") || getDifference(a, b, "Anomal") || a.localeCompare(b) }); function getDifference(a, b, s) { return b.startsWith(s) - a.startsWith(s) } }

      的待定迁移

      此外,如果您使用 SQLite 3.7.9 ,您应该看一下这个discussion

答案 2 :(得分:-5)

您应首先运行rails db:migrate RAILS_ENV=test更新测试数据库。

这样做的原因是,Rails将尝试加载架构,而不仅仅是在测试架构有挂起的迁移时提出。现在只有在加载了模式之后存在挂起的迁移时才会引发异常。

使用此功能时需要注意以下几点:

  • 迁移仍需要手动运行;虽然现在这只需要在“开发”中完成。环境。
  • 如果架构尚未初始化,则会引发异常。该例外将提供说明rake db:migrate需要运行的说明。