Ruby 当参数需要资源引用时,如何rspec测试Puppet类
这可能是一个简单的模仿资源的问题,但是Ruby 当参数需要资源引用时,如何rspec测试Puppet类,ruby,unit-testing,rspec,puppet,rspec-puppet,Ruby,Unit Testing,Rspec,Puppet,Rspec Puppet,这可能是一个简单的模仿资源的问题,但是 class myclass ( String stringParam, Integer intParam, File fileParam ) { # do some things $path = fileParam['title'] $mode = fileParam['mode'] # do some more things } 现在我想为这个类编写一个rspec puppet测试。如何创建或模拟文件资源,并将其放入rspec p
class myclass (
String stringParam,
Integer intParam,
File fileParam
) {
# do some things
$path = fileParam['title']
$mode = fileParam['mode']
# do some more things
}
现在我想为这个类编写一个rspec puppet测试。如何创建或模拟文件资源,并将其放入rspec puppet使用的目录中,以便引用它
答案让我半途而废,但我所尝试的一切都导致myClass抱怨它被传递了一个字符串而不是一个文件引用
...
let(:params) {{
:stringParam => 'Here is my string',
:intParam => 238,
:fileParam => *??????,*
}}
rspec puppet对此并没有太多支持,因为类测试参数列表是从
:params
生成的,假设只使用字符串(或包含字符串的嵌套哈希/数组等)或两个允许的符号值,:undef
和:default
。它没有传递资源引用的方法
存在一种变通方法,可以通过传递响应inspect
方法的对象,将文字内容放入清单中。例如,在spec\u helper.rb
中:
class ResourceReference
def initialize(ref)
@ref = ref
end
def inspect
@ref
end
end
然后在您的规范中:
let(:params) {{
:stringParam => 'Here is my string',
:intParam => 238,
:fileParam => ResourceReference.new("File[/etc/foo]"),
}}
rspec puppet将调用ResourceReference
对象上的inspect
方法,该方法返回传入的字符串。这应该放在清单中,保持不变
(这最初用作undef
的变通方法,现在可以作为:undef
传递)
另一方面,您可以设置let(:pre_condition){“…”}
在生成的类{…}
之前向测试清单添加文字内容,但我认为这里没有使用这种方法
我强烈建议您针对rspec puppet提交一个功能请求。如果您使用let(:pre-condition)来实例化一些资源,然后使用此解决方案来获取它们的引用,请(一如既往)小心评估顺序;在需要资源之前,不能指望资源的属性存在。