Minitest 中 "def setup" 和 "setup do" 的区别?

Difference between "def setup" and "setup do" in Minitest?

在 Rails Minitests 中调用 def setupsetup do 有什么区别吗?我一直在使用 def setup ,但我突然发现我的 setup 用于特定测试文件没有被调用。当我将其更改为 setup do 时,它突然又起作用了(没有更改任何其他内容)。但我发现这很奇怪,为了保持一致性,如果可能的话,我宁愿坚持使用 def setup。任何建议表示赞赏。

require 'test_helper'
require_relative '../../helpers/user_helper'

class FooTest < ActiveSupport::TestCase
  include UserHelper

  # This method doesn't get called as-is.
  # But it does get called if I change the below to `setup do`.
  def setup
    # create_three_users is a UserHelper method.
    create_three_users
    @test_user = User.first
  end


  test 'should abc' do
    # Trying to call @test_user here returned nil.
  end
end

还有另一个测试文件 class 定义为 class FooTest < ActiveSupport::TestCase。我想有人通过复制原始 FooTest 文件创建了它,但忘了更改名称。

简而言之,调用的是另一个 FooTest 的设置方法,而不是这个方法。巧合的是,另一个 FooTest 也在设置中调用了相同的 create_three_users,这就是为什么我直到尝试分配实例变量但失败后才意识到这一点。

我找不到太多关于 def setupsetup do 之间实际区别的信息,但有一个 blog(你必须相信我的话,因为它是用日语写的) 写道 setup do 不仅调用了 class 的设置过程,还调用了其父 class 的设置过程,这可能解释了为什么我的测试使用 setup do (也许它称为setup 两个 FooTests).