Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda