Chefspec repeatedly loads the libraries and gives the warning "an already initialized constant CONSTANT",

I have a cookbook with a library, for example. library.rb . It contains CONSTANT :

 CONSTANT = 'constant' 

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

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

Warnings appear repeatedly, as many times as the number of examples (test cases) minus one. I think this is because chefspec loads the libraries once for each example. Can someone tell me how to get libraries to load only once or how to disable the warning message?

+5
source share
1 answer

Short term, change it to:

 CONSTANT ||= 'constant' 

In the long run, it is better to use let() or transfer the constant from the test case or choose any other way to replace the constant or ensure that the test code loads the library once, and not many times.

Edit - Good point @sawa in the comments: if your constant is nil or false , then the ||= approach does not stop the warning, so you need a better solution like

 CONSTANT = 'constant' unless defined? CONSTANT 
+6
source

Source: https://habr.com/ru/post/1215506/


All Articles