sinon.mock (). Ожидает (). atLeast ()… ожидание.verify () не работает

1

Я новичок на узле и sinon и у меня возникли проблемы с тестированием компонента ниже. Я хотел бы проверить, res.status ли res.status и res.send внутри компонента.

Компонент для тестирования

module.exports = {

   handle: function(promise, res, next, okHttpStatus) {
       promise
           .then(payload => res.status(okHttpStatus ? okHttpStatus : 200).send(payload))
           .catch(exception => next(exception));
    }
};

Unit тест

const sinon = require("sinon");
const routerPromiseHandler = 
require("../../../main/node/handler/PromiseHandler");

describe("Should handle promisse", () => {

    it("should handle success promise return", () => {

        const successMessage = {message: "Success"};

        const promiseTest = new Promise((resolve, reject) => {
            resolve(successMessage);
        });

        let res = {
            status: function() {},
            send: function() {}
        };

        const mockRes = sinon.mock(res);
        const expectStatus =  mockRes.expects("status").withExactArgs(200).atLeast(1)
        const expectSend =  mockRes.expects("send").withExactArgs(successMessage).atLeast(1)

        const spyNext = sinon.spy();

        routerPromiseHandler.handle(promiseTest, res, spyNext, 200);

        expectStatus.verify();
        expectSend.verify();

    });
});
Теги:
sinon
sinon-chai

1 ответ

0

Мне удалось решить проблему. Проверка на синус не работала, потому что шпионы были вызваны внутри обещания. Чтобы проверить, вызван ли шпион. Я должен был добавить утверждения внутри и уловить обещания.

const sinon = require("sinon");
const { mockResponse } = require("mock-req-res");

const routerPromiseHandler = require("../../../main/node/handler/PromiseHandler");

describe("Should handle promisse", () => {

    it("should handle success promise return", () => {

        const successMessage = { message: "Success" };

        const promiseTest = new Promise((resolve, reject) => {
            resolve(successMessage);
        });

        const mockedRes = mockResponse();

        const spyNext = {};

        routerPromiseHandler.handle(promiseTest, mockedRes, spyNext, 200);

        promiseTest.then(() => {
            sinon.assert.calledWithMatch(mockedRes.status, 200);
           sinon.assert.calledWithMatch(mockedRes.send, successMessage);
        })
    });

    it("should handle error promise return", () => {

        const errorMessage = { error: "error" };

        const promiseError = new Promise((resolve, reject) => {
            reject(errorMessage);
        });

        const mockedRes = mockResponse();
        const nextSpy = sinon.spy();

        routerPromiseHandler.handle(promiseError, mockedRes, nextSpy, 200);

        promiseError
            .then(() => {
                // Promise always need the then
            })
           .catch(exception => {
                sinon.assert.calledWithMatch(nextSpy, errorMessage);
           })
    });
});

Ещё вопросы

Сообщество Overcoder
Наверх
Меню