Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

javascript - using sinon to mock a line in a function

I have a function as follows

import config from "../config";
export const encrypt = (recordJSONy, logger) => {
const key =  config.encryption.key
if(!key || key == ""){
 throw encryptionError.ENCYPTION_KEY_MISSING
}
 try{
const decryptedDataStr = TisEncryption.Cipher.encrypt(recordJSON);
return decryptedDataStr;
} catch(e){
console.log(e)
throw encryptionError.ENCRYPTION_FAILED
}
}

I need to write a test for it

describe("help function test", () => {
it("test", () => {
console.log(helperFunctions.encrypt("xxx", loggingService))
});
});

However the above is always returning error because config.encryption.key returns undefined. I am trying to find a way to kind of mock config.encryption.key to return "xxxxxxx" instead of undefined. The more I look at sinon the more I get confused. Can anyone shed light on how to do so?

question from:https://stackoverflow.com/questions/65838433/using-sinon-to-mock-a-line-in-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...