mercredi 29 avril 2015

Javascript unit tesing, test hitting a callback inside function

I am having trouble getting complete coverage in my testing where I am trying to hit a callback function inside the function I am testing. Here is the function :

CrowdControl.prototype.get = function() {
    var options = this.optionsFor('GET');
    return q.Promise(function(resolve, reject) {
        function callback(error, response, body) {
            if (error) {
                reject(error);
            } else {
                resolve(body);
            }
        }

        request(options, callback);
    });
};

So I have the function covered off except the function callback :

function callback(error, response, body) {
    if (error) {
        reject(error);
    } else {
        resolve(body);
    }
}

I cant seem to figure out how to hit this with tests.

Up top I have the request stubbed out like this

   var request = sinon.stub();

beforeEach(function() {
    CrowdControl = rewire('crowdcontrol');
    CrowdControl.__set__({
        request: request
    });
});

So I'm not sure how I can make it hit the callback and test that. Could use some insight as this is still new to me. Thanks!

So I'm trying a simple test at first something like this -

 it("should call callback function.", function() {
        crowdControl.get();
        //callback should fire?
        expect(callback).to.have.been.called;

    });

Aucun commentaire:

Enregistrer un commentaire