gpt4 book ai didi

ruby-on-rails - 你如何编写一个只为测试文件执行一次的设置方法?

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:32 26 4
gpt4 key购买 nike

我希望有一种方法可以针对每个文件运行一次,而不是针对每个测试运行一次。我已经看到一些对“之前”方法的引用,但似乎不适用于 MiniTest。理想情况下,像这样:

class MyTest < ActiveSupport::TestCase
before do
# Executes once per file
end

setup do
# Executes once per test
end

# Tests go here
end

最佳答案

Before用于minitest使用spec dsl时,相当于setup。您可以使用 setup,如果您在 test_helper.rb 文件中使用 setup,它将在所有测试之前执行一次。

设置也可以在测试类中声明。使用设置,放置一个标志并在第一次更新标志。

x = 0
setup do
if x == 0
x = x + 1
puts "Incremented in x = #{x}"
end
end

setup_executed = false
setup do
unless setup_executed
#code goes here
setup_executed = true
end
end

关于ruby-on-rails - 你如何编写一个只为测试文件执行一次的设置方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33289767/

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