I'm trying to test function which uses 'easy-soap-request' library. I want to mock results returned by 'soapRequest' function.
I've tried this but it didn't worked, I keep getting data from external API.
client.js
const soapRequest = require('easy-soap-request');
async function get_data(){
var response = await soapRequest(url, auth_headers) //this is what I want to mock
var result;
result = some_parsing_function(response); //this is what I want test
return result;
}
test.js
const client = require('../../client');
describe('get_data tests', () =>{
it('should test sth', function (done) {
var stubed = stub(client, 'soapRequest').returns('dummy value');
client.get_data().then((result) => {
//assertions
console.log(result) //result still has value from external service
done();
});
})
});
EDIT:
So I've tried using sinon.fake() as suggested by one of the answers.
const client = require('../../client');
describe('get_data tests', () =>{
it('should test sth', function (done) {
var fake_soap = fake(async () => {
return 12345;
});
replace(cilent, 'soapRequest', fake_soap);
client.soapRequest().then((res) => {
console.log(res); // 12345
})
client.get_data().then((result) => {
//assertions
console.log(result) //still original value from external service
done();
});
})
});