Created
February 25, 2015 17:14
-
-
Save jiverson/567ff0b423367566a937 to your computer and use it in GitHub Desktop.
sinon testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe.skip('handler only', function() { | |
// this suite of tests will show database mocking. if you don't want to mock the database, you will have to | |
// use mf-test-helpers to setup the data you want before running the test (what we've always done) | |
it('calls the route handler directly with mocked data', function(done) { | |
// stub dependencies | |
// stub mfData.query | |
var queryStub = sinon.stub(mfData.db, 'query'); | |
// removeSnapshotForCoursesNoLongerInSeries | |
queryStub.onCall(0).callsArg(2); | |
// getSeriesCoursesCourseStates | |
queryStub.onCall(1).callsArgWith(2, null, fixtures.getSeriesCoursesCourseStates); | |
// updateSeriesCourseStates | |
queryStub.onCall(2).callsArg(2); | |
// removeCertificatesForNonCompletedTrainees | |
queryStub.onCall(3).callsArg(2); | |
// mfData.repositories.filterUsers | |
var filterUsersStub = sinon.stub(mfData.repositories, 'filterUsers') | |
.yields(null, [ 1 ]); | |
// mfData.repositories.inviteTraineesToSeriesCourses | |
var inviteTraineesToSeriesCoursesStub = sinon.stub(mfData.repositories, 'inviteTraineesToSeriesCourses') | |
.yields(); | |
// mfData.repositories.updateSeriesCompletionDeadlines | |
var updateCompletionDeadlineForSeriesStub = sinon.stub(mfData.repositories, 'updateCompletionDeadlineForSeries') | |
.yields(); | |
// prepare input | |
var time = Date.now(); | |
var request = { | |
params: { | |
clientDatestamp: time, | |
seriesId: 1 | |
}, | |
payload: { | |
traineeIds: [1], | |
yammerRefIds: [123] | |
}, | |
auth: { | |
credentials: { | |
accountId: 1, | |
allowedGroups: [], | |
userId: 1 | |
} | |
} | |
}; | |
// invoke handler | |
route.handler(request, function(result) { | |
// check expectations | |
assert.notOk(result); // no result indicates status code 200 | |
// check calls were made to deps | |
sinon.assert.callCount(queryStub, 4); | |
sinon.assert.called(filterUsersStub); | |
sinon.assert.called(inviteTraineesToSeriesCoursesStub); | |
sinon.assert.called(updateCompletionDeadlineForSeriesStub); | |
// check params were correct | |
sinon.assert.calledWith(filterUsersStub, { | |
accountId: 1, | |
allowedGroups: [], | |
userIds: [1] | |
}); | |
sinon.assert.calledWith(inviteTraineesToSeriesCoursesStub, { | |
accountId: 1, | |
seriesId: 1, | |
traineeIds: [1], | |
userId: 1, | |
yammerRefIds: [123] | |
}); | |
sinon.assert.calledWith(updateCompletionDeadlineForSeriesStub, { | |
clientDatestamp: time, | |
seriesId: 1, | |
traineeIds: [1] | |
}); | |
// cleanup stubs | |
queryStub.restore(); | |
filterUsersStub.restore(); | |
inviteTraineesToSeriesCoursesStub.restore(); | |
updateCompletionDeadlineForSeriesStub.restore(); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment