chef-infrachefspec

Chefspec how to unit test for update_apt_update without update


I have the following chef code:

apt_update 'Ubuntu apt repo update' do
  subscribes :nothing, 'apt_repository[some-repo]', :immediately
end

I'm trying to test with this code:

it 'updates apt repo' do
  expect(chef_run).to update_apt_update('Ubuntu apt repo update')
end

Which fails with

expected "apt_update[Ubuntu apt repo update]" with action :update to be in Chef run.

Other apt_update resources: apt_update[Ubuntu apt repo update]

Seems like it's trying to assert my action is update, how do I make it not assume my action?


Solution

  • At the chefspec, you have already told it to expect update_apt_update so it is expecting an update action from the resource. So, it is not assuming your action and not trying to assert anything. chefspec is pretty simple in the sense that it tries to avoid people do things they don't intend to do.

    In the recipe section, please add the action that you are already testing for.

    apt_update 'Ubuntu apt repo update' do
    action :update
    subscribes :nothing, 'apt_repository[some-repo]', :immediately
    end
    

    I always go to these chefspec examples from sethvargo when I need help.

    chefspec examples