我收到错误
NoMethodError
-------------
undefined method `registry_key' for HashOperations:Module
收敛厨师食谱时。
这是来自library/hash_operations.rb的代码的简短版本:
module HashOperations
# Tried: require 'chef/mixin/shell_out'
# Tried: include Chef::Mixin::ShellOut
def self.create_reg_keys(item_hash)
item_hash.each do |item_name, item_props|
# (...) construct the item_keys array
registry_key "HKEY_LOCAL_MACHINE\\SOFTWARE\\#{item_name}" do
recursive true
values item_keys
action :create
end
end
end
def self.generate_reg_keys_for_item_key(...)
# some hash operations here
create_reg_keys(item_hash)
end
end unless defined?(HashOperations)
class Chef::Resource
# Tried: Chef::Resource.send(:include, HashOperations)
# Tried: include HashOperations
extend HashOperations
end
这是recipes/default.rb:
Chef::Resource.send(:include, HashOperations)
ruby_block "test" do
block do
# Tried: Chef::Recipe.send(:include, HashOperations)
items_keys.each do |item_key|
HashOperations.generate_reg_keys_for_item_key(..., item_key)
end
end
end
我猜主要问题来自于尝试在模块内的方法内使用 Chef 资源registry_key,而模块内的方法又从配方中调用。
如果我不使用模块,我有一个工作版本,但如果我想用ChefSpec测试代码,我需要一个模块,正如几篇文章指出的那样(比如这篇文章:Stubbing library class methods in ChefSpec)
上面提到的链接是我在模块内部使用end unless defined?(HashOperations)的原因。
我试过使用包含语句,它可以在评论中看到,或者在食谱的第一行中,正如一些 StackOverflow 帖子所建议的那样,没有运气。一篇文章讨论了 LWRP 的使用,但我真的不认为这里是这种情况,因为代码与这个食谱严格相关,不会在其他食谱中使用。
注意:我正在使用self. 为了使def彼此可见,否则我会收到有关generate_reg_keys_for_item_key不可用的错误。
因此,考虑到我花了很多时间寻找解决方案,包括 StackOverflow 建议的解决方案,问题是:解决此错误的最佳方法是什么,并有一个可以用ChefSpec测试的简单解决方案(虽然我并不完全排除 LWRP),我应该包括什么和如何使registry_key在收敛操作中可见?