Unable to stub a library method #273

Closed
jperry opened this Issue Dec 10, 2013 · 2 comments

Projects

None yet

2 participants

@jperry
jperry commented Dec 10, 2013

Hi,

I have looked through the issues but I don't see a specific case like mine for stubbing the library I have created and need a little guidance as I have a feeling Chef is coming in to play here by overwriting my rspec stub.

I have a recipe that has a method

databag = encrypted_data_bag_item('id', 'item')

I have library file in another cookbook that defines this method and then in the recipe I do something like this to make it available to recipes:

::Chef::Recipe.send(:include, Helpers::EncryptedDataBag)

I would like to mock the call to encrypted_data_bag_item and return a hashed result. I have tried several approaches but none seem to work. I'm getting this failure before the mock as well

     Failure/Error: ChefSpec::Runner.new do |node|
     NameError:
       Cannot find a resource for encrypted_data_bag_item on chefspec version 0.6.1

Not sure where the version 0.6.1 is coming from as I am using chefspec 3.0.1

Any help would be much appreciated.

Thanks,
Jay

@sethvargo
Owner
Chef::Recipe.any_instance.stub(:encrypted_data_bag_item).and_return('...')
@sethvargo sethvargo closed this Dec 10, 2013
@jperry
jperry commented Dec 10, 2013

Ah, it was the 'any_instance' message I was missing. Thanks again Seth!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment