Javascript 使用精确参数调用Sinon js检查存根
使用精确参数调用Sinon js检查存根 要求:我想测试使用正确参数调用的ejs.renderFile 我的函数文件: html_to_pdf_converter.jsJavascript 使用精确参数调用Sinon js检查存根,javascript,node.js,mocha.js,sinon,chai,Javascript,Node.js,Mocha.js,Sinon,Chai,使用精确参数调用Sinon js检查存根 要求:我想测试使用正确参数调用的ejs.renderFile 我的函数文件: html_to_pdf_converter.js var ejsToPdfConvert = function (template, data, callback) { var row = data.voucher; html = ejs.renderFile( path.join(__dirname+'/../../views/', temp
var ejsToPdfConvert = function (template, data, callback) {
var row = data.voucher;
html = ejs.renderFile(
path.join(__dirname+'/../../views/', template),
{
data: data
},
function (error, success) {
if (error) {
callback(error, null);
} else {
var pdfPath = getPdfUploadPath(row);
htmlToPdf.convertHTMLString(success, pdfPath, function (error, success) {
if (error) {
if (typeof callback === 'function') {
callback(error, null);
}
} else {
if (typeof callback === 'function') {
callback(null, success, pdfPath);
}
}
});
}
});
};
Mt测试是:html_to_pdf_converter.test.js
describe("ejs to html converter", function () {
it('ejs to html generation error', function() {
var data = {
voucher: {},
image_path: 'tmp/1.jpg',
date_format: '',
parameters: ''
};
var cb_1 = sinon.spy();
var cb_2 = sinon.spy();
var ejsStub = sinon.stub(ejs, 'renderFile');
var pathStub = sinon.stub(path, 'join');
ejsStub.callsArgWith(2, 'path not found', null);
htmlToPdfConverter.ejsToPdfConvert('voucher', data, cb_1);
sinon.assert.calledOnce(ejs.renderFile);
sinon.assert.calledOnce(path.join);
sinon.assert.calledOnce(cb_1);
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), data, cb_2); //Error in this line
ejsStub.restore();
pathStub.restore();
});
});
这条线有两个问题:
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), data, cb_2);
首先,您希望使用参数'data'调用ejsStub,但当您实际调用renderFile时,您将其包装为:{data:data}
第二个是cb_2不等于实际传递给renderFile的函数(error,success){if(error)…}
要使其正常工作,请按如下方式运行:
sinon.assert.calledWith(ejsStub,path.join('views/',templateName),{data:data})代码>