5

I have a chef cookbook with a library, e.g. library.rb. It contains a CONSTANT:

CONSTANT = 'constant'

When I write unit tests for this cookbook, it always gives me the warning:

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

The warnings come up repeatedly, as many times as the number of examples (test cases) minus one. I think it is because chefspec loads the libraries once for each example. Could someone tell me how to make the libraries load only once, or how to disable the warning message?

4

1 回答 1

6

短期,将其更改为:

CONSTANT ||= 'constant'

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

编辑 - @sawa 在评论中的好点:如果您的常量是nilor false,那么该||=方法不会停止警告,因此您需要更好的解决方案,例如:

CONSTANT = 'constant' unless defined? CONSTANT
于 2015-03-17T08:35:05.110 回答