mercredi 29 avril 2015

node, unit-testing and mocking with sinon

So I am using a test suite of Chai, rewire, sinon, and sinon-chai to test some node javascript. This is my first time trying to set this up so I could use some pointers. The function I am trying to test looks like so :

UserRoles.get = function(ccUrl, namespace, environment, ccToken, authPath) {
    var crowdControl = new CrowdControl(ccUrl, namespace, environment, ccToken, authPath);
    return q.Promise(function(resolve, reject) {
        crowdControl.get().then(resolve).fail(reject).done();
    });
};

Inside a document that exports as UserRoles. So I have the initial set up working fine, where I am having troubles is mocking to test this function. I'm trying to mock the new CrowdContol part so my attempt to do that looks like so : http://ift.tt/1J8CC8v .

so I'm trying out the

testHelpers.sinon.stub(CrowdControl, "UserRoles");

to intercept and stub

var CrowdControl = require('./crowdcontrol');

then just running

userRoles.get;

console.log(CrowdControl);

And it seems the stub is not being called ( it logs it's a stub but not that it has been called). I will also need to stub the crowdControl.get() hopefully too, however I was trying to get this simple part working first. Not sure what I need to be doing differently to get this to work here. This is my first time unit testing in node, I've done a bunch in angular where I could just "mock" the CrowdControl, but I'm not sure how it works in node.

Aucun commentaire:

Enregistrer un commentaire