如何用sinon存根对象方法?

时间:2019-04-22 22:13:23

标签: node.js sinon

我需要将mh对象的sendMandrill方法存根。

查看我的测试文件(mail.js):

let MailHandler = require('../../modules/mail.handler.module');
...
let api = (router, parser) => {
   let send = async (req, res, next) => {
      let mh = new MailHandler();
      mh.sendMandrill();    
      ...
   }
   ...    
   return router.post('/mail/send', parser.json(), send);
}
module.exports = api;
...

我的测试(mail.spec.js):

let stRequest = require('supertest');
let MailHandler = require('../../modules/mail.handler.module');
describe('my test', () => {
   beforeEach(() => {
      sinon.stub(MailHandler.prototype, 'sendMandrill', () => true);
   })
   it('stubs sendMandrill!', done => {
      stRequest(app)
         .post('/mail/send')
            .end((err, resp) => {
                done();
            });
   })
})

当前我遇到以下错误:

TypeError: Cannot stub non-existent own property sendMandrill

添加mail.handler.module-参见下面的mailHandler / sendMandrill代码:

module.exports = mailHandler;

function mailHandler() {
    ...
    var mandrill = require('../modules/mandrill');

    var handler = {
        sendMandrill: sendMandrill,
        ...
    };

    return handler;

    function sendMandrill() {
        mandrill.messages.sendTemplate({
            message: {...}
        });
    }
    ...
}

1 个答案:

答案 0 :(得分:1)

您当前的方法是为sendMandrill工厂创建的每个实例创建一个新的mailHandler。您实际上应该不带新的let mh = mailHandler()来命名它,甚至最好将其重命名为createMailHandler以避免滥用。

如果您想有效地使用原型继承,则需要重写mailHandler才能实际使用this来代替新创建的对象。

var mandrill = require('../modules/mandrill');

module.exports = MailHandler;

function MailHandler() {
    // use this instead of newly created object
    this.foo = 'bar'

    // avoid explicit return
    // return handler;
}

// set methods to prototype
MailHandler.prototype.sendMandrill = function sendMandrill() {
        // use this instead of handler here
        mandrill.messages.sendTemplate({
            message: {...}
        });
    }

使用上述方法,您将能够通过sinon对原型属性进行存根,并证明使用new关键字来调用构造函数是合理的。

UPD

如果您无法控制mail.handler.module,则可以使用rewire模块来模拟整个依赖关系,也可以将MailHandler作为api模块的一部分进行公开它可以注射。

api.MailHandler = require('../../modules/mail.handler.module')

let mh = api.MailHandler();

然后在测试中

let oldMailHandler;

beforeAll(() => { oldMailHandler = api.MailHandler})
afterAll(() => { api.MailHandler = oldMailHandler})
beforeEach(() => { api.MailHandler = function MockMailHandler() {} })
相关问题