2017-06-09 1 views
0

J'ai fait un tracker de présence qui utilise des feuilles de slack et de google, et maintenant j'essaye d'écrire les tests pour cela. Je suis actuellement en train de me moquer de la méthode timesheets.get(). Gardez obtenir leSinon, le simulacre revient une fois (jamais appelé)

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

Expected get(tester, Thu Jun 01 2017 00:00:00 GMT+0900[, ...]) once (never called)

message d'erreur

Code:

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

et timesheets.get() est la suivante:

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

Et voici mon essai

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

Dans le test, je passe deux arguments, ne sais pas pourquoi il montre ce message d'erreur. Quelqu'un pourrait-il souligner ce que je fais mal?

Répondre

0

Modification du mockTimesheets au suivant résolu la question

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) 

;