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
undefined method `[]' for nil:NilClass
My data bag structure is
{
"id": "databagname",
"cardetails": {
"car_model": "ABC",
"car_engine": "XYZ",
"car_type": "DEE",
}
}
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'}
)