javascript - node, unit-testing and mocking with sinon -
so using test suite of chai, rewire, sinon, , sinon-chai test node javascript. first time trying set use pointers. function trying test looks :
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 document exports userroles. have initial set working fine, having troubles mocking test function. i'm trying mock new crowdcontol part attempt looks : https://jsfiddle.net/d5dczyuk/ .
so i'm trying out
testhelpers.sinon.stub(crowdcontrol, "userroles");
to intercept , stub
var crowdcontrol = require('./crowdcontrol');
then running
userroles.get; console.log(crowdcontrol);
and seems stub not being called ( logs it's stub not has been called). need stub crowdcontrol.get() too, trying simple part working first. not sure need doing differently work here. first time unit testing in node, i've done bunch in angular "mock" crowdcontrol, i'm not sure how works in node.
just clarify checking if crowcontrol called vars passing in, should stub it? want mock crowdcontrol can force returns.
edit: here second attempt : https://jsfiddle.net/5m5jwk5q/
i use proxyquire kind of testing. proxyquire can stub out require'd dependencies modules you're trying test. in case do:
var crowdcontrolspy = sinon.spy(); // makes sure when ./user-roles tries require ./crowdcontrol // our controlled spy passed, instead of actual module. var userroles = proxyquire('./user-roles', { './crowdcontrol': crowdcontrolspy }); userroles.get(...); expect(crowdcontrolspy).to.have.been.called;
Comments
Post a Comment