lundi 28 novembre 2016

Testing a function argument which is a function

I'm working on testing some code but I'm having some trouble with sinon. The thing is that one of my functions takes a function as a parameter and I haven't found how to mock it.

Usually you do something like this:

var get = sinon.stub($, 'get')

Then later after using $.get:

sinon.assert.calledWith(get, expectedObject);

My code is as follows:

function getUsers(usersPromise) {
    const config = { date: new Date() };
    return usersPromise(config)
        .then(function (data) {
            // Do stuff
        })
}

What I want to do is to be able to mock usersPromise. So I would check that it was called with the correct config object (I omitted plenty of values) and then also assert some stuff in the .then function.

sinon.stub(usersPromise) won't work so I'm a bit lost.

Aucun commentaire:

Enregistrer un commentaire