ChefSpec: Reboot Logic


I’m struggling to understand how best to implement a spec test for my reboot scenario.

I have the following code which installs a Windows feature using the dsc_resource resource. After each Windows feature is installed, I check the ‘reboot_pending?’ flag to determine if I need to reboot.

[‘web-server’, ‘web-mgmt-console’, ‘web-asp-net45’].each do |feature|
dsc_resource ‘[WindowsFeature]’ + feature do
resource :WindowsFeature
module_name 'PSDesiredStateConfiguration’
module_version '1.1’
property :Name, feature
property :Ensure, 'Present’
if reboot_pending?
notifies :reboot_now, ‘reboot[Application_Requires_Reboot]’, :immediately

If a reboot is detected via the reboot_pending? flag, I dynamically invoke the reboot resource, also defined in the same recipe.

reboot ‘Application_Requires_Reboot’ do
reason 'cause Chef said so!'
only_if { reboot_pending? }

The first challenge that I have is that I don’t know how to stub the ‘reboot_pending?’ method. The other challenge is I don’t know how to write a test for my reboot resource as currently constructed. Since I’m feeding the action dynamically, via the notify function, I don’t know how to articulate this in my test.

Any help on these would be greatly appreciated.