chef-infrachefspec

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


Solution

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