mardi 24 mars 2020

Javascript Mocha/Sinon fake XMLHttpRequest to return object in middle of tested method

Hello so i am new to using mocha/sinon and i have this issue

i am testing this method

export async function onConfigEvent(event: configEvent, otherConfig: otherConfig) {
  var myConfig = event.config as Config;

  const info = await fetchInfo(otherConfig.detailsPath, myConfig.Id);

  if (!isDefined(info)) {
    console.log('info is undefined.');
    return;
  }
}

this method is actually quite much bigger with plenty of if statements so i want to test every path of it. the problem i have is with the fetchInfo() call. this function will call a function that creates an url and so on and then calls another function the makes the actual call with a promise. what i need to to is to fake this fetchInfo and make it return what i want for this instance i want it to return undefined so it goes into the if and in other cases i want it to return a faked model like if it succeeded. but i have no idea on how to do it and i have searched for a long while and started trying with sinon.stub but how would i implement that here?

this is how my test looks so far

it('should log info undefined', () => {

let event: configEvent = {
  type: events.Config,
  config: { ["Id"]: 361 }
}

let fetch = sinon.stub(fetchResource);

let otherConfig = getOtherConfig();

let spy = sinon.spy(console, 'log');

onConfigEvent(event, otherConfig)

assert(spy.calledWith('info is undefined.'));

spy.restore();

});

where fetchResource is the function that fetchInfo calls but this does nothing

Aucun commentaire:

Enregistrer un commentaire