【发布时间】:2017-08-21 07:46:27
【问题描述】:
我创建了一个简单的 Puppet 4 类和一个单元测试,如下所示(在 modules/test/ 中执行 touch metadata.json; rspec-puppet-init 之后):
# modules/test/manifests/hello_world1.pp
class test::hello_world1 {
file { "/tmp/hello_world1":
content => "Hello, world!\n"
}
}
# modules/test/spec/classes/test__hello_world1_spec.rb
require 'spec_helper'
describe 'test::hello_world1' do
it { is_expected.to compile }
it { is_expected.to contain_file('/tmp/hello_world1')\
.with_content(/^Hello, world!$/) }
end
我可以通过在modules/test/ 中执行rspec spec/classes/test__hello_world1_spec.rb 来成功运行单元测试。
我现在想继续学习一个更高级的课程,它使用来自另一个模块的代码,即concat(该模块已经安装在modules/concat):
# modules/test/manifests/hello_world2.pp
class test::hello_world2
{
concat{ "/tmp/hello_world2":
ensure => present,
}
concat::fragment{ "/tmp/hello_world2_01":
target => "/tmp/hello_world2",
content => "Hello, world!\n",
order => '01',
}
}
# modules/test/spec/classes/test__hello_world2_spec.rb
require 'spec_helper'
describe 'test::hello_world2' do
it { is_expected.to compile }
# ...
end
当我在 modules/test 中尝试使用 rspec spec/classes/test__hello_world2_spec.rb 运行此单元测试时,我收到一条错误消息,其中包括:
Failure/Error: it { is_expected.to compile } 编译期间出错: 评估错误:评估资源语句时出错,未知 资源类型:'concat'
我怀疑根本原因是rspec 找不到其他模块,因为它没有被告知“模块路径”。
我的问题是:我应该如何开始单元测试,尤其是那些需要访问其他模块的测试?
【问题讨论】:
标签: unit-testing rspec puppet rspec-puppet