【问题标题】:Sinon mock returns once (never called)Sinon 模拟返回一次(从不调用)
【发布时间】:2017-06-09 10:15:32
【问题描述】:

我制作了一个使用 slack 和 google sheet 的考勤跟踪器,现在我正在尝试为它编写测试。我目前正在努力模拟 timesheets.get() 方法。继续获取

ExpectationError: Unexpected call: get(tester, Thu Jun 01 2017 00:00:00 GMT+0900)

预期的 get(tester, Thu Jun 01 2017 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();
  });
});

在测试中我传递了两个参数,不知道为什么会显示该错误消息。谁能指出我做错了什么?

【问题讨论】:

    标签: javascript unit-testing mocking sinon


    【解决方案1】:

    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)
    
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-24
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2021-01-23
      • 2021-12-11
      • 2018-08-31
      相关资源
      最近更新 更多