Information Technology Reference
In-Depth Information
Share Test Code with spec_helper.rb
ChefSpec supports moving common code used in your tests to a
spec_helper.rb
file, similar
to Serverspec.
As with Serverspec, you'll have to imagine there are many test files in this example, and
we'll move the shared code to
spec_helper.rb
.
Create the file
spec/spec_helper.rb
with the content shown in
Example 16-15
.
We are mov-
ing the
require
and
at_exit
calls to this shared file.
Example 16-15. chefdk/apache-test/spec/spec_helper.rb
require 'chefspec'
at_exit { ChefSpec::Coverage.report! }
Example 16-16. chefdk/apache-test/spec/default_spec.rb
require
'spec_helper'
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 run
rspec
, you should notice no net change in the program output from when you
ran
rspec
in the last section, as all we did was move around some code:
$
rspec --color
.
Finished in 0.00337 seconds (files took 1.11 seconds to load)
1 example, 0 failures
ChefSpec Coverage report generated...
Total Resources: 3
Touched Resources: 1
Touch Coverage: 33.33%