rspec puppet如何测试定义为类成员的类

rspec puppet如何测试定义为类成员的类,rspec,puppet,rspec-puppet,Rspec,Puppet,Rspec Puppet,我有一门课看起来像 class addon { case $::operatingsystem { 'windows': { Dsc_xfirewall { dsc_ensure => 'Present', } } 'RedHat': { } default: { warning "OS : ${::operatingsystem} is not (yet) support

我有一门课看起来像

class addon {
    case $::operatingsystem {
    'windows': {
        Dsc_xfirewall {
            dsc_ensure    => 'Present',
        }
     }
     'RedHat': {
     }
     default: { warning "OS : ${::operatingsystem} is not (yet) supported" }
  }
}
我希望我的测试看起来像

describe 'addon', :type => :class do
    os = 'windows'
    let(:facts) {{:operatingsystem => os}}
    describe os do
        it {
            is_expected.to contain_class('addon').with(
              {
                :Dsc_xfirewall => {
                  :dsc_ensure => 'Present',
                }
              }
            )
        }
    end
end
目录编译正确,为清晰起见,应删除要编译的目录。然而,我似乎无法实现这一点:如果我尝试使用dsc\u xfirewall相同的故事,并且

contains_dsc_xfirewall
我得到一个错误,即dsc_防火墙不是有效的定义。有人知道如何更好地组织我的考试吗?在任何人指出如果目录编译正确,我不需要这个测试之前,我知道这一点;这只是更复杂事物的一个简化版本


因此,我的问题是:为了检查类是否包含dsc\u xfirewall以及所有参数是否设置正确,测试必须是什么样子的?

明显的问题是,清单没有声明任何实际资源,而Rspec似乎认为清单实际上声明了

此代码如下:

    Dsc_xfirewall {
        dsc_ensure => 'Present',
    }
声明自定义类型的资源默认值()。我猜
Present
中的大写
P
也是一个打字错误

我还注意到,您将
let(:facts)
语句放错了位置,并且您打开了另一个
descripe
块,我认为您应该在其中使用
context

我按照我认为您正在尝试的方式编写了一些代码,以说明清单和Rspec代码应该是什么样子:

(我更改了一些内容,以便在Mac上轻松编译。)

Rspec:

describe 'foo', :type => :class do
  context 'Darwin' do
    let(:facts) {{:operatingsystem => 'Darwin'}}
    it {
      is_expected.to contain_class('foo')
    }
    it {
      is_expected.to contain_file('/tmp/foo').with(
        {
          :ensure => 'file',
        }
      )
    }

    # Write out the catalog for debugging purposes.
    it { File.write('myclass.json', PSON.pretty_generate(catalogue)) }
  end
end

显然,您将使用
contain\u file
而不是
contain\u dsc\u xfirewall

我认为您的
JSON有一个输入错误。pretty\u generate
模块方法使用。非常感谢您提供的提示,所以我实际上正在尝试测试一些无法测试的东西。我可以在实际使用dsc_xirewall的地方测试dsc_xirewall,这是我还没有想到的。仍在努力弄清楚哪些是可以测试的,哪些是不可以测试的。请查看由我提供的代码编译的目录。RSpecPuppet基本上是对目录中的内容进行断言。
describe 'foo', :type => :class do
  context 'Darwin' do
    let(:facts) {{:operatingsystem => 'Darwin'}}
    it {
      is_expected.to contain_class('foo')
    }
    it {
      is_expected.to contain_file('/tmp/foo').with(
        {
          :ensure => 'file',
        }
      )
    }

    # Write out the catalog for debugging purposes.
    it { File.write('myclass.json', PSON.pretty_generate(catalogue)) }
  end
end