Search code examples
chef-infrachefspec

chefspec testing for stray ruby variable


Over here I am returning status of provider in variable, how can I mock that status in chefspec for testing ?

ret = yum_package 'blah' do
   action :install
end   
cookbook_file "/etc/init.d/blah" do
   source "blah"
   only_if { ret.updated_by_last_action? }
end

Solution

  • I doubt this work, the ret.update_by_last_action? is evaluated at compile time when the provider didn't run.

    The recommended way to do this would be to use notifications like this:

    yum_package 'blah' do
       action :install
       notifies :create,"cookbook_file[/etc/init.d/blah]", :immediately
    end   
    
    cookbook_file "/etc/init.d/blah" do
       action :nothing
       source "blah"
    end
    

    And then you can expect the notification is sent and that the file is created.

    Spec example:

    blah_package = chef_run.yup_package('blah')
    expect(blah_package).to notify('cookbook_file[/etc/init.d/blah]').to(:create).immediately
    expect(chef_run).to render_file('/etc/init.d/blah')