如何在 Rspec 中按修改时间对测试进行排序
How to sort test by modification time in Rspec
如何覆盖 Rspec 默认顺序 运行 按文件修改时间测试?
给你加上这个配置就可以了 test_helper
/spec_helper
/rails_helper
:
RSpec.configure do |config|
config.register_ordering(:global) do |items|
items.sort_by { |item| -File.mtime(item.metadata[:absolute_file_path]).to_i }
end
end
说明: Rspec 允许您使用 config.resgister_orderdering
定义自定义排序。在这种情况下,我们将覆盖全局配置。您所要做的就是传递一个块,您可以在其中定义自定义排序函数。
item.metadata[:absolute_file_path]
获取测试文件的文件路径。
File.mtime
获取一个文件的修改时间,减号为倒序
如何覆盖 Rspec 默认顺序 运行 按文件修改时间测试?
给你加上这个配置就可以了 test_helper
/spec_helper
/rails_helper
:
RSpec.configure do |config|
config.register_ordering(:global) do |items|
items.sort_by { |item| -File.mtime(item.metadata[:absolute_file_path]).to_i }
end
end
说明: Rspec 允许您使用 config.resgister_orderdering
定义自定义排序。在这种情况下,我们将覆盖全局配置。您所要做的就是传递一个块,您可以在其中定义自定义排序函数。
item.metadata[:absolute_file_path]
获取测试文件的文件路径。File.mtime
获取一个文件的修改时间,减号为倒序