Reputation: 138
I'm attempting to write units tests for a server method that generates an excel report file in node js.
I'm using the excel4node
library to generate the report. The basic call chain to fill out a cell in this library is the following:
let workbook = new excel4node.Workbook();
let worksheet = report.addWorksheet("worksheetName");
worksheet.cell(1, 1).string("Content in cell A1").style(styleObject);
In my testing code, I've gone with the approach of just creating a big stubbed object to account for all of these method chain calls. It's not a perfect approach by any means, but this is what I have so far (which is functional for now):
styleStub = sinon.stub().returns({style: sinon.stub()});
cellStringStub = sinon.stub().returns({style: styleStub});
cellNumberStub = sinon.stub().returns({style: styleStub});
cellStub = sinon.stub().returns({
string: cellStringStub,
number: cellNumberStub,
style: styleStub,
});
// Stub xl.Workbook.addWorksheet.cell method with our defined stubs so that we can
// assert any calls
xlStub = sinon.stub(xl, "Workbook").returns({
addWorksheet: sinon.stub().returns({
cell: cellStub,
column: sinon.stub().returns({
setWidth: sinon.stub()
}),
}),
createStyle: sinon.stub().returns({}),
});
I'm assigning the stubs to variables so I can use the sinon spy functions on them for asserting. What I'm struggling with is asserting on the method chain calls of this library when looking at given parameters. Given some test data, I know exactly what should be put into a given cell.
e.g. If I know cell A5 should contain the number 142, the method chain call would look like:
worksheet.cell(1, 5).number(142).style({someStylingObjectImNotConcernedAbout});
Is there a way to assert that the parameter given to the number() method call is 142, given the parameters of the preceeding cell(1, 5) call are 1 and 5?
Thanks!
Upvotes: 0
Views: 23