I'm new Sinon. i'm unable to spy the private ajax function
Actual code in library.js
function ajax () {
console.log("I'm a");
}
function getJSON() {
ajax();
console.log("I'm b");
}
exports.getJSON = getJSON;
Actual Test Code
var sinon = require('sinon');
var lib = require('./library');
describe('Tutor Test', function () {
it('getJSON Calling ajax', function (done) {
var ajax = sinon.spy(lib, 'ajax');
lib.getJSON();
ajax.restore();
sinon.assert.calledOnce(ajax);
done();
});
});
Note: I already tried with below object example. it works like charm.
Working Code in library.js
var jquery = {
ajax: function () {
console.log("I'm a");
},
getJSON: function () {
this.ajax();
console.log("I'm b");
}
};
exports.jquery = jquery;
Working Test Cases.
var sinon = require('sinon');
var $ = require('./library').jquery;
describe('Tutor Test', function () {
it('getJSON Calling ajax', function (done) {
var ajax = sinon.spy($, 'ajax');
$.getJSON();
ajax.restore();
sinon.assert.calledOnce(ajax);
done();
});
});
I'm getting error like below during mocha test
1) Tutor Test getJSON Calling ajax:
TypeError: Attempted to wrap undefined property ajax as function
at Object.wrapMethod (node_modules/sinon/lib/sinon/util/core.js:113:29)
at Object.spy (node_modules/sinon/lib/sinon/spy.js:41:26)
at Context.<anonymous> (test.js:41:26)
ajaxexported in yourlibrary.js- Vsevolod Golovizninyes i know. that is the question without exportingajax` which is not required for application but needed for test case.` - Sachinsinondoes not work for functional code - Sachinlibrary.jsmodule. - Hosar