Information Technology Reference
In-Depth Information
require
'chefspec'
describe
'apache::default'
ddo
let
(
:chef_run
) {
ChefSpec
:
:Runner
.
new
.
converge
(
described_recipe
) }
it
'installs apache2'
ddo
expect
(
chef_run
)
.
to install_package
(
'httpd'
)
end
end
end
end
Change the source in
default_spec.rb
to use the
let()
helper method as shown in
Example 16-13. chefdk/apache-test/spec/default_spec.rb
require
'chefspec'
describe
'apache-test::default'
ddo
let
(
:chef_run
) {
ChefSpec
:
:Runner
.
new
.
converge
(
described_recipe
) }
it
'installs apache2'
ddo
expect
(
chef_run
)
.
to install_package
(
'httpd'
)
end
end
end
end
When you rerun
rspec
, you should notice no net change in the test results:
$
rspec --color
.
Finished in 0.00042 seconds (files took 1.12 seconds to load)
1 example, 0 failures
Generate a Coverage Report
Another ChefSpec helper method is
ChefSpec::Coverage.report!
. It will generate a list of
resources that have corresponding examples as documentation. You can let this report guide
your testing.
Ruby that permits you to register a block to execute when the program exits. In this case, we
want to run the
ChefSpec::Coverage.report!
method. The exclamation point (!) in the
report!
method name is a Ruby convention that indicates a method is
dangerous
. In this