jeudi 30 avril 2015

Javascript unit testing, force error in sinon stub callback

I am trying to test one last bit of this function I cannot seem to hit correctly. Here is the function

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

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

And the part I can't seem to hit is

  if (error) {
            reject(error);
        } else {
            resolve(body);
        }

I have the request set as a stub, and here is what I am trying

   it("should call callback, which should reject if errors.", function() {
            var testCallback = testHelpers.stub();
            request.returns("Error");
            crowdControl.get(testCallback);
            //expect(testCallback).to.have.been.called;
            expect(testCallback).to.have.been.calledWith("Error");
        });

Seems like it is not working as I expected, I need to test the callback throwing an error. Thanks!

Aucun commentaire:

Enregistrer un commentaire