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

175
Visualizações
Sinon Spy not being called with right arguments

Background

I am trying to learn how to do a RESTful API following the TDD paradigm by reading a book on the subject (it is in brazillian):

  • https://leanpub.com/construindo-apis-testaveis-com-nodejs/read

The author encourages the use a sinon.js together with mocha.js.

I am getting close to the end, but I am failling to pass the test for my gnomeController.

Problem

The problem is that I am using sinon to assert that I am calling the gnomeController's get method using the given reponse object, which is in reallity a spy.

This spy is to make sure I call the reponse method with an "Error", but it appears I am calling the response with no arguments whatsoever, which is very confusing.

Code

gnomeController.js

module.exports = aGnomeModel => {

    let Gnome = aGnomeModel;

    function get(req, res){
        return Gnome.find({})
            .then(gnomes => res.send(gnomes))
            .catch(err => res.status(400).send(err));
    }

    return Object.freeze({
        get
    });
};

gnomeTest.js

const sinon = require("sinon");
const gnomesControllerFactory = require("gnomesController.js");
const Gnome = require("gnomeModel.js");

describe("Controllers: Gnomes", () => {

    describe("get() gnomes", () => {

        it("should return 400 when an error occurs", () => {
            const request = {};
            const response = {
                send: sinon.spy(),
                status: sinon.stub()
            };

            response.status.withArgs(400).returns(response);
            Gnome.find = sinon.stub();
            Gnome.find.withArgs({}).rejects("Error");

            const gnomesController = gnomesControllerFactory(Gnome);

            return gnomesController.get(request, response)
                .then(arg => {
                    console.log(arg);
                    sinon.assert.calledWith(response.send, "Error");
                });
        });
    });

});

Question

I am using the latest versions of both libraries.

  1. What is wrong in my code, why is the reponse being called with no arguments?
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Solution

After much debugging, I found out that the solution is to replace:

 function get(req, res){
        return Gnome.find({})
            .then(gnomes => res.send(gnomes))
            .catch(err => res.status(400).send(err));
    }

with:

 function get(req, res){
        return Gnome.find({})
            .then(gnomes => res.send(gnomes))
            .catch(err => res.status(400).send(err.name));
    }

Which is not explained in the book. Kinda wish I could give more feedback on it, but so far it is what it is.

over 4 years ago · Santiago Trujillo 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