I have the following resource in one my recipes
download_dir = "#{Chef::Config[:file_cache_path]}\\BESClient"
task_done = "#{Chef::Config[:file_cache_path]}\\BESClient\\installed.txt"
file task_done do
content Date.today.to_s
action :create_if_missing
end
Corresponding to this I have written the following chef spec test
context 'Windows 2012R2: when all attributes are default' do
let(:chef_run) do
runner = ChefSpec::ServerRunner.new(platform: 'windows', version: '2012R2', file_cache_path: 'C:\Program Files (x86)\BigFix Enterprise')
runner.converge(described_recipe)
end
it 'converges successfully' do
expect { chef_run }.to_not raise_error
end
download_dir = 'C:\\Program Files (x86)\\BigFix Enterprise\\BESClient'
task_done = 'C:\\Program Files (x86)\\BigFix Enterprise\\BESClient\\installed.txt'
it 'creates a file with attributes' do
expect(chef_run).to create_file_if_missing(task_done.to_s).with(
content: Date.today.to_s)
end
This tries to create C:\Program Files (x86)\BigFix Enterprise
directory on my workstation but if I remove file_cache_path
variable from the Runner the unit test fails with the following error
1) besclient::windows Windows 2012R2: when all attributes are default creates
a file with attributes
Failure/Error:
expect(chef_run).to create_file_if_missing(task_done.to_s).with(
content: Date.today.to_s
)
expected "file[C:\Program Files (x86)\BigFix Enterprise\BESClient\install
ed.txt]" with action :create_if_missing to be in Chef run. Other file resources:
file[C:/Users/AKANKS~1/AppData/Local/Temp/chefspec20180413-11784-1dp9wh
rfile_cache_path\BESClient\installed.txt]
anyone help how to test this scenario?
By default chefspec sets a new temporary directory for file caching in every run which may not be existing in your machine. To avoid that we need to set file_cache_path. Unless this is specified your spec test will fail. You can set this in your spec_helper.rb file to avoid repetition in each spec file
RSpec.configure do |config|
config.file_cache_path = Chef::Config[:file_cache_path]
end