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

223
Vistas
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 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