使用 sqlite 启动 rspec 测试 :memory: - "migrations are pending"

Launch rspec tests with sqlite :memory: - "migrations are pending"

我想 运行 使用 rspec 在我的 rails 应用程序上 ruby 上使用 sqlite 和内存数据库进行测试。但是,每次我启动 rspec 时,它都会告诉我迁移正在等待,如果我 运行 之前已经进行了迁移。有没有办法在我 运行 测试之前每次都进行迁移? 这是我的数据库配置

test:
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  database: ":memory:"

您需要在测试中加载模式而不是依赖迁移。

按照this blogpost中的建议替换

ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb"