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

224
Visualizações
How do I use the calledOnceWithExactly method on a stub?

I am unit testing a module which imports another function as a dependency. In the unit test, I've used the Sinon package to stub that dependency. I'm now using Sinon's calledOnceWithExactly method to check that this dependency is called exactly once, with a specific parameter passed to it. It seems like my call to calledOnceWithExactly("items") should return true, but it is returning false--Sinon seems to think the dependency was called without passing any parameters at all. Any ideas why? My code is below.

More Info

  • In the test file, the value of createModelStub.calledOnce is 1, confirming the function is called exactly once.
  • If I change calledOnceWithExactly("items") to calledOnceWithExactly(), then that returns true. As if Sinon thinks the function was called with no parameters.
  • I've done this succesfully in the past, in another project, using this exact same approach. But that was maybe ~6-12 months ago by now, so maybe things changed.
  • I had a look through Sinon documentation, but couldn't see anything of note. Thanks in advance.

My Code

Test file: client/test/unit-tests.js

import test from "tape"; // assign the tape library to the variable "test"
import sinon from "sinon";
import { saveToDb } from "../src/module-save-to-db.js";
import createModel from "../../server/models/createModel.js";

test("Test of save to database.", async function (t) {
  const createModelStub = sinon.stub(createModel, "createModel").returns(555);
  await saveToDb();
  console.log(`createModelStub.calledOnce`);// Prints 1.
  console.log(createModelStub.calledOnceWithExactly("items"));// Prints false, even though createModel is indeed called once with "items" as a parameter.
  t.end();
});

Module under test: client/src/module-save-to-db.js

import { createModel } from "../../server/models/createModel.js";
export function saveToDb() {
  return createModel("items").then((myModel) => {// createModel called and passed "items" as a parameter.
    const model = new myModel({
      number: 555,
      all_items: "value1"
    });
    return true;
  });
}

Dependency module is at: server/models/createModel.js

about 4 years ago · Juan Pablo Isaza
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