gpt4 book ai didi

ruby - Chefspec 重复加载库并发出警告 "already initialized constant CONSTANT"

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

我有一本带图书馆的 Chef Recipe ,例如库.rb。它包含一个 CONSTANT:

CONSTANT = 'constant'

当我为这本 Recipe 编写单元测试时,它总是给我警告:

(Some prefix...)warning: already initialized constant CONSTANT
(Some prefix...)warning: previous definition of CONSTANT was here

警告反复出现,次数为示例(测试用例)减一。我认为这是因为 chefspec 为每个示例加载一次库。谁能告诉我如何让库只加载一次,或者如何禁用警告消息?

最佳答案

短期,将其更改为:

CONSTANT ||= 'constant'

从长远来看,最好使用let(),或者将常量移出测试用例,或者选择任何其他替换常量的方式,或者确保测试代码加载库一次,而不是多次。

编辑——@sawa 在评论中提出的要点:如果您的常量是nilfalse,则||= 方法不会停止警告,因此您需要更好的解决方案,例如:

CONSTANT = 'constant' unless defined? CONSTANT

关于ruby - Chefspec 重复加载库并发出警告 "already initialized constant CONSTANT",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29093461/

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