通过绑定到click函数,我能够弄清楚如何测试this.toJSON函数。但是我对如何测试this.download()函数有些迷惑。 (这是与AmCharts相关的自定义导出)
it('should call toJSON on export', function() {
var exp = chart.export.menu[0].menu[0].menu[0];
/*
exp = {
label: 'JSON',
click: function() {
this.toJSON({}, function(data) {
this.download(
data,
'application/json',
this.setup.chart.$exportName + '.json'
);
});
}
}
*/
var self = { toJSON: jasmine.createSpy() };
exp.click.bind(self)();
expect(exp.label).toEqual('toJSON');
expect(self.toJSON).toHaveBeenCalled();
});
答案 0 :(得分:0)
经过一段时间的讨论,这似乎对我来说很好...
// before each I set everything up
var dlcb = {
setup: {
chart: chart
},
download: jasmine.createSpy('download')
};
var self = {};
['toCSV', 'toJSON', 'toXLSX'].forEach(function(name) {
self[name] = jasmine
.createSpy(name, function(a, cb) {
cb.bind(dlcb)(true);
})
.and.callThrough();
});
// -----
it('should call toJSON on export', function() {
var exp = chart.export.menu[0].menu[0].menu[0];
/*
exp = {
label: 'JSON',
click: function() {
this.toJSON({}, function(data) {
this.download(
data,
'application/json',
this.setup.chart.$exportName + '.json'
);
});
}
}
*/
var self = { toJSON: jasmine.createSpy() };
exp.click.bind(self)();
expect(exp.label).toEqual('toJSON');
expect(self.toJSON).toHaveBeenCalled();
expect(dlcb.download).toHaveBeenCalledWith(true, 'application/json', 'vp-chart-export.json');
});