有没有办法用测试值存根方法调用? 例如,我希望get_folder_name在我的 ChefSpec 测试期间返回"test">
directory 'Log_Folder' do
def get_folder_name
'c:tempfolder'
end
action :create
path get_folder_name
end
试过这个,但它不能替换值。
before do
allow_any_instance_of(Chef::Resource).to receive(:get_folder_name).and_return('test')
end
所以问题可能是因为该方法是这样内联声明的,所以它会一直覆盖存根。您可以尝试allow_any_instance_of(Chef::Resource::Directory)
或类似方法,但我可能会将此逻辑移动到自定义资源中,在该资源中可以更自然地存根。