使用精确参数调用的 Sinon js 检查存根
Sinon js check stub called with exact arguments
使用确切参数调用的 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/', 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();
});
});
这条线有 2 个问题:
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), data, cb_2);
首先,您希望使用参数 'data' 调用 ejsStub,但是当您实际调用 renderFile 时,您将它包装成这样:{data: data}
.
第二个是 cb_2 不等于 function (error, success) { if (error) ... }
你实际传递给 renderFile。
要让它正常工作 运行 像这样:
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), {data: data});
不需要传递cb_2或其他任何东西,因为实际的回调是在函数中定义的,不能更改。
使用确切参数调用的 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/', 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();
});
});
这条线有 2 个问题:
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), data, cb_2);
首先,您希望使用参数 'data' 调用 ejsStub,但是当您实际调用 renderFile 时,您将它包装成这样:{data: data}
.
第二个是 cb_2 不等于 function (error, success) { if (error) ... }
你实际传递给 renderFile。
要让它正常工作 运行 像这样:
sinon.assert.calledWith(ejsStub, path.join('views/', templateName), {data: data});
不需要传递cb_2或其他任何东西,因为实际的回调是在函数中定义的,不能更改。