Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
Sinon Spy on all function calls within a function

Say we have a function like this in flow.js which is a web component so all are functions inside a class within this file.

change(){
   
   this.getParams();
   this.loadData();
}

I want to write a test case that can test change() and confirm whether these two functions have been invoked. I tried many approaches but it dint work. One of the approaches I tried is:

  it('change() test', function () {
     const sandbox = sinon.createSandbox();
     sandbox.spy(el);
     el.change();
     assert(el.change.calledOnce); // output is true
     assert(el.getParams.calledOnce); // output is false
     assert(el.loadData.calledOnce); // output is false
  }

el is the object/webcomponent.

Could you please let me know what is wrong here and what is the right implementation for this scenario ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should add spy to el.getParams() and el.loadData() methods.

E.g.

flow.js:

class Flow {
  change() {
    this.getParams();
    this.loadData();
  }
  getParams() {}
  loadData() {}
}

module.exports = Flow;

flow.test.js:

const Flow = require('./flow');
const sinon = require('sinon');

describe('flow', () => {
  it('should pass', () => {
    const sandbox = sinon.createSandbox();
    const flow = new Flow();
    sandbox.spy(flow, 'getParams');
    sandbox.spy(flow, 'loadData');
    flow.change();
    sinon.assert.calledOnce(flow.getParams);
    sinon.assert.calledOnce(flow.loadData);
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda