Reputation: 5783
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",
}
}
Upvotes: 0
Views: 586
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
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