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



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

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!

update

这是我的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

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

  1. config/application.rb
  2. 配置/环境/test.rb
  3. spec/rails_helper.rb

因此,您要收到的迁移待处理错误必须归因于config.active_record.migration_error = true此配置设置在Rails Engine上的某个位置,该设置在Rails Engine上加载rails_helper.rb,其中定义了ActiveRecord::Migration.maintain_test_schema!指令。

尝试在 config/emoverments/test.rb 上设置config.active_record.migration_error = false,如RSPEC升级指南中所述。

可能是由于两个原因。

  1. 您可能已经错过了在config/environments/test.rb中配置的

如果没有它,则添加config.active_record.maintain_test_schema = true如果您将其设置为false

,请将其设置为true

来自 docs

config.active_record.maintain_testrongchema是一个布尔值 控制主动记录是否应尝试保持测试数据库 运行时使用db/schema.rb(或db/structure.sql(最新架构 您的测试。默认值为真。

  1. 您可能有在架构加载后未决迁移

来自 rspec docs

这样做的是,而不仅仅是在测试模式时提高 有未决的迁移,Rails将尝试加载模式。 现在,只有在有待处理的迁移时才提高例外 之后,已加载模式。

检查您是否有rake db:migrate:status

的待迁移

另外,如果您使用的是 sqlite 3.7.9 ,则应查看此讨论

您应该运行rails db:migrate RAILS_ENV=test首先更新测试DB。

这是这样做的,而不仅仅是在测试架构进行待处理的迁移时提高,Rails会尝试加载模式。现在,只有在尚未加载架构的待处理迁移时,才会提高例外。

使用此信息时有一些警告要注意:

  • 迁移仍需要手动运行;尽管现在只需要在"开发"环境中完成。
  • 如果尚未初始化架构,将提出一个例外。例外将提供指示rake db:migrate需要运行的说明。

相关内容

  • 没有找到相关文章

最新更新