0
votes

I'm trying to write the unit test for chef and stubbing the encrypted data bag as below. Recipe part

variables(car_model: Chef::EncryptedDataBagItem.load('databagname', node.environment, key_name)['cardetails']['car_model'])

Doing stub in below pattern

Chef::EncryptedDataBagItem.stub(:load).with('databagname', 'test', 'somekey').and_return(
"cardetails": {
  'car_model' => 'abc'
  }
)

Getting error as undefined method

NoMethodError

undefined method `[]' for nil:NilClass

Cookbook Trace:

My data bag structure is

{ "id": "databagname", "cardetails": { "car_model": "ABC", "car_engine": "XYZ", "car_type": "DEE", } }

2

2 Answers

0
votes

It got resolved when i'm using below syntax for stubbing

Chef::EncryptedDataBagItem.stub(:load).with('databagname', 'test', 'somekey').and_return(
  "cardetails" => {'car_model' => 'abcasasasasas'}
  )
0
votes

In general you shouldn't be using that API if you can help it. The normal data_bag_item API automatically decrypts if needed, and then you could use the normal stub helpers in ChefSpec.