sinon.mock (). Ожидает (). atLeast () ... ожидание.verify () не работает - PullRequest
0 голосов
/ 10 октября 2018

Я новичок на узле и sinon, и у меня возникают проблемы с тестированием компонента ниже.Я хотел бы проверить, были ли вызваны 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));
    }
};

Юнит-тест

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();

    });
});

1 Ответ

0 голосов
/ 11 октября 2018

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

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);
           })
    });
});
...