gpt4 book ai didi

ruby-on-rails - Minitest 5 - 套件级设置

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:16 24 4
gpt4 key购买 nike

如何使用 minitest 5 运行套件级别的设置和拆卸(在运行所有测试之前和之后)?我正在尝试复制为 rails 3 编写的自定义测试运行器的功能,目前将其升级到 rails 4。

这似乎是 Ruby Minitest: Suite- or Class- level setup? 的副本但 .runner 的功能在 minitest 5.0+ 中已被弃用

例如,我希望这些在所有测试之前和之后运行。

def before_suites
# code to run before the first test
p "Before everything"
end

def after_suites
# code to run after the last test
p "After everything"
end

最佳答案

我一直在寻找类似的解决方案。这是 minitest README file 提出的解决方案:

describe Blah do
SETUP = begin
# ... this runs once when describe Blah starts
end
# ...
end

我试过这个,但由于我使用了一些 test_helper 支持方法,所以它对我来说不太奏效。我写了这个简单的解决方法:

class SearchServiceTest < ActiveSupport::TestCase
@@init = 0

def setup
if @@init.eql?(0)
.. setup code goes here ..

@@init = 1
end
end

...
end

不是最优雅的解决方案,但对我有用。

关于ruby-on-rails - Minitest 5 - 套件级设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32726759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com