如何在默认 Rails 中自定义测试生成器?
How do I customise test generators in default Rails?
编辑:最初这是一个 Rails 5.2 的问题,但它似乎仍然与 Rails 6 和 6.1
相关
我正在尝试自定义在 vanilla Rails 5.2.x 应用程序中默认生成哪些测试。不过,我似乎无法配置 Minitest 生成器。当我尝试生成模型时,config/application.rb
中的以下代码导致 error minitest [not found]
错误。
config.generators do |generate|
generate.test_framework :minitest, model: false
end
我的Assumptions/Understanding
在Rails 5.2中默认的测试框架是Minitest and Capybara
Minitest gem 自动包含在 rails new
中
您可以 customise generator behaviour 在 config/application.rb
中使用 config.generators do
块
test_framework
"defines which test framework to use. Defaults to false and will use Minitest by default"
显然我的理解有误,或者我的假设之一是错误的。
重现步骤
rails -v
# Rails 5.2.1.1
rails new testing-test-frameworks
# Bundle install outputs: 'Using minitest 5.11.3'
cd testing-test-frameworks
rails g model --help
# Default [test framework]: test_unit [why???]
rails g model person
# invoke test_unit [why???]
# create test/models/person_test.rb
# create test/fixtures/people.yml
奇怪的是,official Rails testing guide 在单元测试和系统测试中也有很多对 invoke test_unit
的引用。
我尝试过的其他东西
我尝试了 config.generators
块中的其他设置,只是为了确保它正常工作
鉴于 test_unit 生成器似乎没有商量余地,我尝试通过 test_unit 更改生成器; config.generators.test_framework :test_unit, model: false
我试图禁用各种其他测试; fixture:false, integration: false, model: false, spec: false, system: false, view: false
我试图确认我没有任何其他 gem 或设置可能会弄乱东西(几个 similar sounding problems 与旧版本的 FactoryBot/Girl)
我尝试了其他 answers
中提到的 generate.test_framework :mini_test
等变体
问题
- 如何控制这些发电机?
- 我应该去哪里(在 Internet 上或在我的文件系统上)了解可以传递给每个测试框架的生成器的有效选项?
- 为什么我的原始应用程序中的帮助系统说默认测试生成器是 TestUnit 而 official documentation 说它是 Minitest?
我试图根据最近的学习(并基于 Rails 6)部分回答我自己的一些问题(2 年多后)。如果我错了,希望坎宁安定律成立。
1。 :test_unit == :minitest (或多或少)
让我感到困惑的第一个大点是 Minitest 确实是引擎盖下的默认测试框架,但 Rails 中的几乎所有内容(还是?)似乎都指的是 TestUnit/test_unit。
示例包括:
rails g model --help
输出(下面的片段)
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: test_unit
^^^^^^^^^
TestUnit options:
^^^^^^^^
rails g scaffold Example
输出(以下片段):
create app/models/example.rb
invoke test_unit
^^^^^^^^^
create test/models/example_test.rb
invoke scaffold_controller
create app/controllers/examples_controller.rb
invoke test_unit
^^^^^^^^^
create test/controllers/examples_controller_test.rb
create app/helpers/examples_helper.rb
invoke test_unit
^^^^^^^^^
- 我们生成测试的方式:
bin/rails generate test_unit:model article title:string body:text
^^^^^^^^^
2。深入测试单元
果然,如果我们查看 all.rb
we see that "Test Unit" is being included via the test_unit
Railtie. If (like me) you don't really know what a Railtie is, check out this blog post,我发现它很有帮助。
就我们可以在 config/application.rb
config.generators
块中提供的选项而言,我认为我们可以通过 config.generators.test_framework :test_unit
传递的唯一选项是 fixture: true/false
(也许根据 test_unit railtie).
中明显的默认值指定 fixture_replacement
路径(?)
3。 “TestUnit”还能生成什么?
如果您 运行 rails g
获取可用生成器的列表,输出中只提到了四个 TestUnit:
TestUnit:
test_unit:channel
test_unit:generator
test_unit:mailbox
test_unit:plugin
但是 Rails Testing Guide 也提到了 test_unit:model
和 test_unit:scaffold
。当我尝试 rails g test_unit:foo
Rails 时,也许我的意思是 rails g test_unit:job
所以这是三个“未记录”的生成器。
据我所知,这三个临时演员来自 this generators folder,还有其他一些。如果是这样,我认为完整的列表是:
test_unit:channel
*不在该文件夹中,似乎是由
ActionCable
test_unit:controller
test_unit:generator
test_unit:helper
test_unit:integration
test_unit:job
test_unit:mailbox
*不在该文件夹中,似乎是由 ActionMailbox 添加的
test_unit:mailer
test_unit:model
test_unit:plugin
test_unit:scaffold
test_unit:system
编辑:最初这是一个 Rails 5.2 的问题,但它似乎仍然与 Rails 6 和 6.1
相关我正在尝试自定义在 vanilla Rails 5.2.x 应用程序中默认生成哪些测试。不过,我似乎无法配置 Minitest 生成器。当我尝试生成模型时,config/application.rb
中的以下代码导致 error minitest [not found]
错误。
config.generators do |generate|
generate.test_framework :minitest, model: false
end
我的Assumptions/Understanding
在Rails 5.2中默认的测试框架是Minitest and Capybara
Minitest gem 自动包含在
中rails new
您可以 customise generator behaviour 在
中使用config/application.rb
config.generators do
块test_framework
"defines which test framework to use. Defaults to false and will use Minitest by default"
显然我的理解有误,或者我的假设之一是错误的。
重现步骤
rails -v
# Rails 5.2.1.1
rails new testing-test-frameworks
# Bundle install outputs: 'Using minitest 5.11.3'
cd testing-test-frameworks
rails g model --help
# Default [test framework]: test_unit [why???]
rails g model person
# invoke test_unit [why???]
# create test/models/person_test.rb
# create test/fixtures/people.yml
奇怪的是,official Rails testing guide 在单元测试和系统测试中也有很多对 invoke test_unit
的引用。
我尝试过的其他东西
我尝试了
config.generators
块中的其他设置,只是为了确保它正常工作鉴于 test_unit 生成器似乎没有商量余地,我尝试通过 test_unit 更改生成器;
config.generators.test_framework :test_unit, model: false
我试图禁用各种其他测试;
fixture:false, integration: false, model: false, spec: false, system: false, view: false
我试图确认我没有任何其他 gem 或设置可能会弄乱东西(几个 similar sounding problems 与旧版本的 FactoryBot/Girl)
我尝试了其他 answers
中提到的generate.test_framework :mini_test
等变体
问题
- 如何控制这些发电机?
- 我应该去哪里(在 Internet 上或在我的文件系统上)了解可以传递给每个测试框架的生成器的有效选项?
- 为什么我的原始应用程序中的帮助系统说默认测试生成器是 TestUnit 而 official documentation 说它是 Minitest?
我试图根据最近的学习(并基于 Rails 6)部分回答我自己的一些问题(2 年多后)。如果我错了,希望坎宁安定律成立。
1。 :test_unit == :minitest (或多或少)
让我感到困惑的第一个大点是 Minitest 确实是引擎盖下的默认测试框架,但 Rails 中的几乎所有内容(还是?)似乎都指的是 TestUnit/test_unit。
示例包括:
rails g model --help
输出(下面的片段)-t, [--test-framework=NAME] # Test framework to be invoked # Default: test_unit ^^^^^^^^^ TestUnit options: ^^^^^^^^
rails g scaffold Example
输出(以下片段):create app/models/example.rb invoke test_unit ^^^^^^^^^ create test/models/example_test.rb invoke scaffold_controller create app/controllers/examples_controller.rb invoke test_unit ^^^^^^^^^ create test/controllers/examples_controller_test.rb create app/helpers/examples_helper.rb invoke test_unit ^^^^^^^^^
- 我们生成测试的方式:
bin/rails generate test_unit:model article title:string body:text ^^^^^^^^^
2。深入测试单元
果然,如果我们查看 all.rb
we see that "Test Unit" is being included via the test_unit
Railtie. If (like me) you don't really know what a Railtie is, check out this blog post,我发现它很有帮助。
就我们可以在 config/application.rb
config.generators
块中提供的选项而言,我认为我们可以通过 config.generators.test_framework :test_unit
传递的唯一选项是 fixture: true/false
(也许根据 test_unit railtie).
fixture_replacement
路径(?)
3。 “TestUnit”还能生成什么?
如果您 运行 rails g
获取可用生成器的列表,输出中只提到了四个 TestUnit:
TestUnit:
test_unit:channel
test_unit:generator
test_unit:mailbox
test_unit:plugin
但是 Rails Testing Guide 也提到了 test_unit:model
和 test_unit:scaffold
。当我尝试 rails g test_unit:foo
Rails 时,也许我的意思是 rails g test_unit:job
所以这是三个“未记录”的生成器。
据我所知,这三个临时演员来自 this generators folder,还有其他一些。如果是这样,我认为完整的列表是:
test_unit:channel
*不在该文件夹中,似乎是由 ActionCabletest_unit:controller
test_unit:generator
test_unit:helper
test_unit:integration
test_unit:job
test_unit:mailbox
*不在该文件夹中,似乎是由 ActionMailbox 添加的
test_unit:mailer
test_unit:model
test_unit:plugin
test_unit:scaffold
test_unit:system