【问题标题】:sinon.mock().expects().atLeast() ... expectation.verify() not workingsinon.mock().expects().atLeast() ...expectation.verify() 不工作
【发布时间】:2018-10-10 14:26:24
【问题描述】:

我是 node 和 sinon 的新手,我无法测试下面的组件。我想检查是否在组件内部调用了 res.statusres.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();

    });
});

【问题讨论】:

    标签: javascript node.js sinon sinon-chai


    【解决方案1】:

    我设法解决了这个问题。 sinon 检查不起作用,因为间谍是在承诺中被调用的。检查是否调用了间谍。我必须在 then 和 catch 中添加断言。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2019-06-28
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      相关资源
      最近更新 更多