0
我正在努力單元測試以下函數。特別是我不能讓它覆蓋突出顯示的代碼:使用帶有磁帶和Sinon的ForEach的單元測試JavaScript函數
function authCert(trustedCAFile){
let ca = [];
let cert = [];
let trustedCA = String(fs.readFileSync(trustedCAFile));
let trustedCALines = trustedCA.split("\n");
trustedCALines.forEach(function(entry){
cert.push(entry);
if(entry.match(/-END CERTIFICATE-/)){
**ca.push(cert.join("\n"));
cert = [];**
}
});
return ca;
}
這是我目前的測試:
let test = require('tape');
let rewire = require('rewire');
let sinon = require('sinon');
let fs = require('fs');
let proxyquire = require('proxyquire');
test('should cycle through trusted ', function(t) {
let authCert = rewire('../authCert');
let getAuthCerts = certUtils.getAuthCert;
let certStub = 'test';
let fsStub = {
readFileSync:() => {}
};
let ca = [];
ca.push('1');
certUtils.__set__('fs', fsStub);
let result = getAuthorisedCerts(certStub);
t.deepEquals(result, []);
t.end();
});
我已經看過的文件,但作爲一個小白,我不知道該如何去獲得覆蓋並測試突出顯示的代碼。
如果任何人都可以幫助我或指出我朝着正確的方向,我會非常感激。
感謝