Мое требование - смоделировать функцию обратного вызова, которая находится внутри аргументов другой функции для модульного тестирования.
var jwt = require("jsonwebtoken");
var APICall = require('./requestFile');
function Helper(){
this.request = new APICall();
}
Helper.prototype.getData = function (done) {
var headers = {
'content-type': "application/json",
};
var _self = this;
this.request.get(this.pub_URL, get_headers, function (err, res, body) {
if (!err && res.statusCode === 200) {
console.log("Got data: ", body);
done(null, body);
}
else {
console.log("Error occured while fetching data: " + err)
done(err, null);
}
});
}
}
Я хочу смоделировать функцию обратного вызова, которую this.request.get () вызывает какаргумент, так что мое тестирование может охватить блок else console.log («Произошла ошибка при извлечении данных:» + err).
Вот мой тестовый файл с базой кода
const Helper = require('../Helper');
var APICall = require('../requestFile');
let hlp = new Helper();
describe('APP', function() {
before(function() {
let res = {
statusCode: 500
}
let err = {
message: 'errors present'
};
var get_headers = {
'content-type': "application/json",
};
sinon.stub(APICall.prototype, 'get').callsFake(function(done) {
done(err, res)
})
})
after(function() {
APIRequester.prototype.get.restore();
});
it('should tell errors when request gets called for', function(done) {
hlp.getData(function(err, data) {
expect(data).to.be.a('string')
done()
})
})
})