【问题标题】:Node js mail jet method chaining stubNode js mail jet方法链接存根
【发布时间】:2017-02-09 12:25:38
【问题描述】:

在这里,我想断言回调函数是否被调用。那么我该如何使用 sinon js 来做到这一点。请提出建议。

var send = function (templateId, callback) {

    const request = mailjet
        .post("send")
        .request(params)
    request
        .then((result) => {
            if (typeof callback === 'function') {
                callback(null, result.body);
            }

        })
        .catch((err) => {
            if (typeof callback === 'function') {
                callback(err, null);
            }
        })
    } else {
        callback(err, null);
    }
};

我试过如下:

var mailjetClient = require('../../node_modules/node-mailjet/mailjet-client');

sinon.stub(mailjet, 'post').withArgs('send').returns(mailjetClient);
sinon.stub(mailjetClient, 'request').returns(Promise);

我收到以下错误:

TypeError: Attempted to wrap undefined property request as function

【问题讨论】:

  • Wah ghar na bhuva ne ghar na dakla。 ;P

标签: node.js mocha.js sinon chai mailjet


【解决方案1】:

试试这个就行了,你必须指定post方法返回什么,然后return并请求return。

   var cb = sinon.spy();

   var obj = {successId: 12121};  // Any success response

   var then = sinon.stub().callsArgWith(0, obj).returns(
        {
            catch: sinon.stub()
        }
    );

    var request = sinon.stub().returns(
        {
            then: then
        }
    );

    sinon.stub(mailjet, "post", () => {
        return {
            request: request
        };
    });

    mailSender.send(templateName, cb);
    sinon.assert.calledOnce(cb);
    mailjet.post.restore();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多