首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Rails在我运行测试时创建许多测试模式?

为什么Rails在我运行测试时创建许多测试模式?
EN

Stack Overflow用户
提问于 2021-04-25 20:49:55
回答 1查看 86关注 0票数 0

当我在我的模型上运行一些测试时,Rails将在MySQL上再创建四个测试模式。因此,在运行:$ bin/rails test之后,Rails生成了这四种模式。

我也有blog_development,没关系.

我做了迁移,填充了固定装置,并编写了一个示例测试。这里有一个示例应用程序:https://github.com/rgiaviti/poc-so-rails-creating-many-test-schemas

下面是我的一些代码:

迁徙

代码语言:javascript
复制
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

固定装置

代码语言:javascript
复制
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

测试

代码语言:javascript
复制
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: Rails 6.1.3.1
  • RVM版本: rvm 1.29.12;
  • Ruby版本:Ruby3.0.0p0 (2020-12-25修订版95aff21468) x86_64-linux;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-25 21:20:26

在您的test/test_helper.rb中有以下指令,我认为它是初始默认测试套件创建的一部分:

代码语言:javascript
复制
parallelize(workers: :number_of_processors)

这是一个在版本6的核心Rails中引入的特性,它创建独立的测试模式,这样多个测试可以并行运行,而不会干扰彼此的数据库访问。

有关这方面的更多信息,请参见Rails文档BigBinary上特性的编写。从医生那里:

对于每个进程,将创建一个新的数据库,其后缀为员工编号。 测试数据库-0测试-数据库-1

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67258165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档