Mahattam
Mahattam

Reputation: 5783

How to stub two level of json data bag in chef spec

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", } }

Upvotes: 0

Views: 586

Answers (2)

coderanger
coderanger

Reputation: 54267

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.

Upvotes: 0

Mahattam
Mahattam

Reputation: 5783

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'}
  )

Upvotes: 0

Related Questions