Reputation: 343
I'm new Sinon
. i'm unable to spy the private ajax
function
library.js
function ajax () {
console.log("I'm a");
}
function getJSON() {
ajax();
console.log("I'm b");
}
exports.getJSON = getJSON;
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.
library.js
var jquery = {
ajax: function () {
console.log("I'm a");
},
getJSON: function () {
this.ajax();
console.log("I'm b");
}
};
exports.jquery = jquery;
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)
Upvotes: 3
Views: 6048
Reputation: 29109
Call your method from the exported public API.
var jquery = {
ajax: function () {
console.log("I'm a");
},
getJSON: function () {
//this.ajax();
jquery.ajax(); // <---
console.log("I'm b");
}
};
Another approach is to call console.log with a hint, and then spy on the log.
ajax: function () {
console.log("I'm a");
},
var ajax = sinon.spy(console, 'log');
$.getJSON();
sinon.assert.calledOnceWithExactly("I'm a")
Upvotes: 0
Reputation: 7746
Sinon
. So avoid using Sinon
for those use cases.Note: Your (
ajax
) function does not have param and return value and not bind to the exported object is real challenge forsinon
In such case, If you wanted to make sure whether ajax
function is triggered or not. You can use rewire
.
Below is the working code.
var rewire = require('rewire');
var lib = rewire('./library');
const assert = require('assert');
describe('Tutor Test', function () {
it('getJSON Calling ajax', function (done) {
lib.__set__('isAjaxCalled', false);
lib.__set__('ajax', function () {
lib.__set__('isAjaxCalled', true);
});
lib.getJSON();
assert.equal(lib.__get__('isAjaxCalled'), true);
done();
});
});
No Change your Actual code, library.js
Upvotes: 4