2

Is there a way to stub a method call with a test value? For example, I want get_folder_name to return 'test' during my ChefSpec test

directory 'Log_Folder' do
    def get_folder_name
      'c:\temp\folder'
    end

    action :create
    path get_folder_name
end

Tried this but it doesn't replace the value.

before do
  allow_any_instance_of(Chef::Resource).to receive(:get_folder_name).and_return('test')
end
chief7
  • 14,263
  • 14
  • 47
  • 80

1 Answers1

0

So the issue is probably that because the method is declared in-line like that, it keeps overwriting the stub. You could try allow_any_instance_of(Chef::Resource::Directory) or similar, but I would probably move this logic to a custom resource where it can be stubbed slightly more naturally.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Adding "::Directory" didn't work. What if I move the method to the recipe instead of the resource? I tried using allow_any_instance_of(Chef::Recipe) but that didn't work either. – chief7 Apr 04 '17 at 10:39