ruby – 如何设置全局RSpec元数据?
发布时间:2020-12-16 21:07:34 所属栏目:百科 来源:网络整理
导读:我有一个脚本包含RSpec 3.4.4中的测试并导致它们在十秒后超时. TIMEOUT = 10RSpec.configure do | config | config.around do |example| timeout = Float(example.metadata[:timeout]) rescue TIMEOUT begin Timeout.timeout(timeout) { example.run } rescu
我有一个脚本包含RSpec 3.4.4中的测试并导致它们在十秒后超时.
TIMEOUT = 10 RSpec.configure do | config | config.around do |example| timeout = Float(example.metadata[:timeout]) rescue TIMEOUT begin Timeout.timeout(timeout) { example.run } rescue Timeout::Error skip "Timed out after #{timeout} seconds" end end end 此脚本位于中心位置 – ?/ lib / spec_helper.rb – 并且是我的存储库中spec_helpers所必需的. 我希望能够在整个存储库级别配置example.metadata [:timeout],使其所有规范在两秒后超时(例如),或者(对于另一个示例)完全没有. 我已经尝试将它设置为.rspec中的一个选项 – 这对我来说是理想的解决方案 – 但当然它不能识别这样的自定义选项.我希望命令行能做同样的事情. 有没有办法为测试套件中的所有示例设置元数据? 解决方法
除了攻击RSpec内部,这可能不是一个好主意,你可以做到这一点的唯一方法是滥用可用的选项:
标签选项是一个很好的选择,因为它允许您输入键/值对.这样做的好处是它可以在.rspec文件中设置,并且可以通过命令行参数覆盖.例如, .rspec配置 --format documentation --color --tag timeout:10 --require spec_helper 命令行 rspec --tag timeout:2 您只需要小心并确保从过滤器中删除标签或者所有测试都将被过滤掉…要使用它,在您的情况下,您只需执行以下操作: RSpec.configure do | config | timeout = config.filter.delete(:timeout) || 10 config.around do |example| begin Timeout.timeout(timeout) { example.run } rescue Timeout::Error skip "Timed out after #{timeout} seconds" end end end 在此特定示例中,将timeout设置为零将禁用超时的使用. 从最高到最低的优先顺序是命令行arg> .rspec配置>在配置块中指定的默认值. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |