用RSpec测试Puppet自定义函数,为什么';我看不到因素变量吗?
根据,我应该能够通过调用lookupvar访问事实用RSpec测试Puppet自定义函数,为什么';我看不到因素变量吗?,rspec,puppet,Rspec,Puppet,根据,我应该能够通过调用lookupvar访问事实 module Puppet::Parser::Functions newfunction(:machinelist2, :type => :rvalue) do lookupvar('operatingsystem') end end 在spec中使用这个函数,它返回nil context 'something' do let(:facts) {{:operatingsystem => 'blah'}} i
module Puppet::Parser::Functions
newfunction(:machinelist2, :type => :rvalue) do
lookupvar('operatingsystem')
end
end
在spec中使用这个函数,它返回nil
context 'something' do
let(:facts) {{:operatingsystem => 'blah'}}
it "should return os" do
result = scope.function_machinelist2([])
result.should(eq('blah'))
end
end
(和运行规范…)
$rspec
即使删除let(:facts)条目,我也希望facter变量解析为当前操作系统
在我的rspec puppet测试中,它肯定会为“常规”puppet代码(类/模块清单)提供支持
更新:
我错过了文件中说:
要使用有关客户机的事实,请使用lookupvar(“事实名称”)而不是
Facter['FACT NAME'].值。如果事实不存在,则使用lookupvar
返回:
因此,我尝试了相反的方法,使用Facter['operatingsystem'].value。
这将返回我当前的操作系统,但我无法使用let()覆盖它
所以,我的问题是,我应该如何覆盖facter变量来测试使用它们的自定义函数(facts)??我对基本问题没有深入了解,但是我怀疑您的
let
调用没有任何效果,因为let
方法是惰性评估的,并且您似乎没有使用rspec puppet gem中可能使用facts
方法的任何自定义匹配器。说到这里,我假设scope
和lookupvar
都不是来自那块宝石。我没有洞察到基本的木偶问题,但是我怀疑您的let
调用没有任何效果,因为let
方法是惰性评估的,并且您似乎没有使用rspec puppet gem中可能使用facts
方法的任何自定义匹配器。说到这里,我假设scope
和lookupvar
都不是来自这个gem。我偶然发现了一些示例,现在看来很明显,由于自定义函数的“原始ruby”性质,存根可以工作
e、 g
scope.stubs(:lookupvar).with(“interfaces”).returns('eth0,lo')
这里有一个更完整的例子
require 'spec_helper'
require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
describe "machinelist2 function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
it "should exist" do
Puppet::Parser::Functions.function("machinelist2").should == "function_machinelist2"
end
context 'something' do
it "should return os" do
scope.stubs(:lookupvar).with("operatingsystem").returns('blah')
result = scope.function_machinelist2([])
result.should(eq('blah'))
end
end
果然
rjames@ma-dt-rj:facts$ rspec
machinelist2 function
should exist
something
should return os
Finished in 0.12552 seconds
2 examples, 0 failures
所以。。。只要我能在运行实际的puppet代码(在rspec之外)时依靠lookupvar()工作,那么我认为这应该非常有效。我偶然发现了一些示例,现在看来很明显,由于自定义函数的“原始ruby”性质,存根可以工作 e、 g scope.stubs(:lookupvar).with(“interfaces”).returns('eth0,lo') 这里有一个更完整的例子
require 'spec_helper'
require 'puppetlabs_spec_helper/puppetlabs_spec/puppet_internals'
describe "machinelist2 function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
it "should exist" do
Puppet::Parser::Functions.function("machinelist2").should == "function_machinelist2"
end
context 'something' do
it "should return os" do
scope.stubs(:lookupvar).with("operatingsystem").returns('blah')
result = scope.function_machinelist2([])
result.should(eq('blah'))
end
end
果然
rjames@ma-dt-rj:facts$ rspec
machinelist2 function
should exist
something
should return os
Finished in 0.12552 seconds
2 examples, 0 failures
所以。。。只要我能够在运行实际的puppet代码(在rspec之外)时依靠lookupvar()工作,那么我认为这应该非常有效。我一直很高兴地在rspec puppet中使用相同的let语法。在清单中,对$::operatingsystem的引用将计算为我在测试中的let中设置的任何值。我的问题是关于测试自定义函数。列出的查找因子变量的方法是使用lookupvar('operatingsystem')。但我似乎不能以同样的方式在测试中获得设置。感谢您对rspec傀儡gem的评论和参考。我已经更新了答案,期待您的反馈。我一直很高兴地在rspecpuble中使用相同的let语法。在清单中,对$::operatingsystem的引用将计算为我在测试中的let中设置的任何值。我的问题是关于测试自定义函数。列出的查找因子变量的方法是使用lookupvar('operatingsystem')。但我似乎不能以同样的方式在测试中获得设置。感谢您对rspec傀儡gem的评论和参考。我已经更新了答案,期待您的反馈。您知道,
stubs
来自摩卡,而不是RSpec。另外,我仍然不相信你在这段代码中使用了let(:facts).
。当您删除let(:facts)
调用时,示例是否仍然通过?是。。。很抱歉let(:facts)在我的新案例中完全没有使用。我会更新的。你知道,stubs
来自摩卡,而不是RSpec。另外,我仍然不相信你在这段代码中使用了let(:facts).
。当您删除let(:facts)
调用时,示例是否仍然通过?是。。。很抱歉let(:facts)在我的新案例中完全没有使用。我会更新的。