Hi I have a ChefSpec test below, I have looked around on Google but could not find the best way to manipulate the node attributes (from recipe) in different context blocks in ChefSpec file. Please let me know if there is a better way to change the node attributes in 'context' level or 'it' block level?
require 'spec_helper'
iis = {
sites: [
{.....,
app_pools: [....]
}
]
}
iis_override = {
site: {...}
}
describe 'myCookbook::iis' do
let(:chef_run) do
ChefSpec::SoloRunner.new do |node|
node.default['is_dev_machine'] = false
end.converge(described_recipe)
# Case 1: node['iis_override'] doesnt exist
end
# There are 2 cases here:
# 1: node['iis_override'] exists or not null
# 2: node['iis_override'] does not exist
iis[:sites].each do |site|
context "if node['iis_override'] DOESNT exist" do
it 'adds the default app pool' do
expect(chef_run).to add_iis_pool('DefaultAppPool').with(
...
)
end
## More complicated 'it' blocks below
end
context "if node['iis_override'] DOES exist" do
#How to correctly set node attribute for iis_override?
#These 2 lines NOT WORKING because they only work in 'it' block
#However, I want to change the node attribute in the context level because
# I have many 'it' blocks under this context and dont want to duplicate this process
chef_run.node.normal['iis_override'] = iis_override
chef_run.converge(described_recipe)
it 'adds the default app pool' do
expect(chef_run).to add_iis_pool('DefaultAppPool').with(
...
)
end
## More complicated 'it' blocks below similar to the previous context
end
end
end
Thanks