You need to enable JavaScript to run this app. 运行Rails测试时为何会在MySQL生成多个多余测试schema?
优惠活动
大模型
产品
解决方案
定价
更多

运行Rails测试时为何会在MySQL生成多个多余测试schema?

Rails 执行测试时在MySQL中额外生成多个测试schema

我在编写模型对应的测试用例并执行测试流程时,Rails会在MySQL数据库中额外创建四个测试schema。执行$ bin/rails test命令后,即可在数据库列表中看到这四个由Rails自动生成的测试schema。

我本地已存在的blog_development开发库运行状态完全正常。
我已经完成全部数据库迁移、fixtures测试数据填充,也编写了基础的示例测试用例,同时在公开代码仓库提供了可复现该问题的完整示例应用。

迁移文件

class CreateArticles < ActiveRecord::Migration[6.1]
  def up
    create_table :articles do |t|
      t.string :title, null: false
      t.text :body, null: false

      t.timestamps
    end
  end

  def down
    drop_table :articles
  end
end

class CreateComments < ActiveRecord::Migration[6.1]
  def up
    create_table :comments do |t|
      t.references :article, null: false
      t.text :body, null: false

      t.timestamps
    end

    add_foreign_key :comments, :articles
  end

  def down
    remove_foreign_key :comments, :articles
    drop_table :comments
  end
end

测试夹具

article-1:
  title: This is the Article 1
  body: Lorem ipsum dolor sit amet, consectetur adipiscing elit. In in erat tortor. Proin et dolor vitae erat blandit molestie. Ut vehicula finibus felis, tempor accumsan justo faucibus vitae

article-2:
  title: This the Article 2
  body: Lorem ipsum dolor sit amet, consectetur adipiscing elit. In in erat tortor. Proin et dolor vitae erat blandit molestie.


comment-1:
  article: article-1
  body: A Comment

comment-2:
  article: article-1
  body: "Some Comment"

comment-3:
  article: article-2
  body: Comment for other article

测试代码

require "test_helper"

class ArticleTest < ActiveSupport::TestCase
  test "should not save article without title" do
    article = Article.new
    article.body = "Body for an Article"
    assert_not article.save
  end
end

运行环境

  • MySQL 8.0.24,通过Docker容器运行
  • Rails 6.1.3.1
  • RVM 1.29.12
  • Ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

内容的提问来源于stack exchange,提问作者humungs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 22:06:02