我制作了一个使用松弛和谷歌表的考勤跟踪器,现在我正在尝试为它编写测试。我目前正在努力模拟timesheets.get()方法。继续得到
ExpectationError:意外的电话:get(测试员,2017年6月1日星期四 00:00:00 GMT + 0900)
预计得到(测试者,2017年6月1日星期四00:00:00 GMT + 0900 [,...])一次 (从未打过电话)
错误消息
码
execute(username, body) {
let user = get username from body;
let year = get year from body;
let month = get month from body;
let calculateMonthTotal = this._getMonthTotal(user, month, year, this.timesheets);
this.slack.send(calculateMonthTotal);
}
static _getMonthTotal(username, month, year, timesheets) {
const date = moment({year: year, month: month, day: 1});
let totalWorkedHours = 0;
while (date.month() == month) {
const row = timesheets.get(username, date);
totalWorkedHours += parseFloat(row.getWorkedHours());
date.add('1','days');
}
return "month total is "+totalWorkedHours;
}
,timesheets.get()
如下:
get(username, date) {
var sheet = this._getSheet(username);
var rowNo = this._getRowNo(username, date);
if (rowNo <= 4) {
return null;
}
var row = sheet.getRange("A"+rowNo+":"+String.fromCharCode(65 + this.scheme.columns.length - 1)+rowNo).getValues()[0].map(function(v) {
return v === '' ? undefined : v;
});
if (row) {
return new TimesheetRow(username, date, row);
}
}
这是我的测试
describe('CommandMonthTotalSpec', ()=> {
it('should call slack send method with expectMessage', () => {
const username = "tester";
const expectMessage = 'month total is 8';
const body = "getMonthTotal "+username+" 2017/6";
const date = moment({year: 2017, month: 5, day: 1});
const row = new TimesheetRow(username, date, ["2017/06/01 00:00:00","2017/06/01 10:00:00","2017/06/01 19:00:00","","1","8","",""]);
const slack = new Slack();
const timesheets = new Timesheets();
let mockTimesheets = sinon.mock(timesheets).expects('get').once().withArgs(username, date).onCall(0).returns(row);
const command = new CommandMonthTotal(slack, null, timesheets);
const mockSlack = sinon.mock(slack).expects('send').once().withArgs(expectMessage);
command.execute(username, body);
mockSlack.verify();
mockTimesheets.verify();
});
});
在测试中我传递了两个参数,不知道为什么它会显示错误信息。谁能指出我做错了什么?
答案 0 :(得分:0)
将mockTimesheets
更改为以下内容解决了问题
let mockTimesheets = sinon.mock(timesheets).expects('get').atLeast(28).atMost(31)
.onCall(0).returns(row)
.onCall(1).returns(null)
.onCall(2).returns(null)
.onCall(3).returns(null)
.onCall(4).returns(null)
.onCall(5).returns(null)
.onCall(6).returns(null)
.onCall(7).returns(null)
.onCall(8).returns(null)
.onCall(9).returns(null)
.onCall(10).returns(null)
.onCall(11).returns(null)
.onCall(12).returns(null)
.onCall(13).returns(null)
.onCall(14).returns(null)
.onCall(15).returns(null)
.onCall(16).returns(null)
.onCall(17).returns(null)
.onCall(18).returns(null)
.onCall(19).returns(null)
.onCall(20).returns(null)
.onCall(21).returns(null)
.onCall(22).returns(null)
.onCall(23).returns(null)
.onCall(24).returns(null)
.onCall(25).returns(null)
.onCall(26).returns(null)
.onCall(27).returns(null)
.onCall(28).returns(null)
.onCall(29).returns(null)
;