io non sono in grado di stub costruttore moment
quando si chiama con la funzione format
per restituire una stringa predefinito, ecco un esempio spec che mi piacerebbe correre con mocha
:Stub moment.js costruttore con Sinon
it('should stub moment', sinon.test(function() {
console.log('Real call:', moment());
const formatForTheStub = 'DD-MM-YYYY [at] HH:mm';
const momentStub = sinon.stub(moment(),'format')
.withArgs(formatForTheStub)
.returns('FOOBARBAZ');
const dateValueAsString = '2025-06-01T00:00:00Z';
const output = moment(dateValueAsString).format(formatForTheStub);
console.log('Stub output:',output);
expect(output).to.equal('FOOBARBAZ');
}));
sono in grado di vedere questo output utilizzando console.log
:
Real call: "1970-01-01T00:00:00.000Z"
Stub output: 01-06-2025 at 01:00
Ma poi il test fallisce causa 01-06-2025 at 01:00 !== 'FOOBARBAZ'
Come posso correttamente stub cheChiamata?
Sembra che questa soluzione non è più necessario. Vedere [questo] (https://github.com/moment/moment/pull/451) cambiare –