sinon.spy(func)即使在被调用时也不会产生调用

时间:2015-11-13 22:52:55

标签: javascript node.js sinon sinon-chai

我有一个正在监视剔除方法的测试。该方法伪造了依赖性。我可以验证该方法实际上是被调用的。我甚至可以验证它是否被预期的参数调用。但是,在查看truecalled时,间谍未提供calledWith

代理mailerStub依赖项的mailer方法。

我甚至按照预期为console.logspy.displayName放置了mailer

我在这里做错了什么?

var proxyquire = require('proxyquire');
var expect = require('chai').expect;
var sinon = require('sinon');
var mockReq = require('./../mockReq');
var mockRes = require('./../mockRes');
var utils = require('./../utils');
var config = require('./../../server-config');

var mailerErr;
var mailerTemplate;
var mailerStub = function mailer(to, subject, html, callback) {
  callback(mailerErr);
};

var emailTemplatesErr;
var templateErr;
var templateHtml;
var emailTemplatesStub = function emailTemplates(root, callback) {
  callback(emailTemplatesErr, function template(name, locals, callback) {
    callback(templateErr, templateHtml);
  })
};

var sut = proxyquire('./../../middleware/sendEmail', {
  '../mailer': mailerStub,
  'email-templates': emailTemplatesStub
});

describe('middleware/sendEmail', function sendEmailSpec() {
  beforeEach(function() {
    mailerErr = false;
    mailerTemplate = undefined;

    emailTemplatesErr = false;
    templateErr = false;
    templateHtml = undefined;
  });

  it('should send an email', function(done) {
    var req = new mockReq();
    var res = new mockRes(next);

    res.email = {
      templateName: 'template-name',
      to: 'test@test.com',
      subject: 'some subject',
      locals: {
        firstName: 'test'
      }
    };

    templateHtml = 'HTML';

    var spy = sinon.spy(mailerStub);
    sut(req, res, next);

    function next(msg) {
      console.log(spy.displayName);

      expect(msg).to.be.false;
      expect(spy.called).to.be.true;
      //expect(spy.calledWith(res.email.to, res.email.subject, templateHtml)).to.be.true;

      done();
    }
  });
});

1 个答案:

答案 0 :(得分:0)

为了帮助将来的某个人,我想出了问题所在。存根需要使用该函数进行设置,然后存根需要注入proxyquire配置。

这可能是因为它不是我正在观看的object.func,而是现有的功能。

var proxyquire = require('proxyquire');
var expect = require('chai').expect;
var sinon = require('sinon');
var mockReq = require('./../mockReq');
var mockRes = require('./../mockRes');
var utils = require('./../utils');
var config = require('./../../server-config');

var mailerErr;
var mailerTemplate;
function mailerStub(to, subject, html, callback) {
  callback(mailerErr);
}
var mailerStubSpy = sinon.spy(mailerStub);

var emailTemplatesErr;
var templateErr;
var templateHtml;
var emailTemplatesStub = function emailTemplates(root, callback) {
  callback(emailTemplatesErr, function template(name, locals, callback) {
    callback(templateErr, templateHtml);
  })
};

var sut = proxyquire('./../../middleware/sendEmail', {
  '../mailer': mailerStubSpy,
  'email-templates': emailTemplatesStub
});

describe('middleware/sendEmail', function sendEmailSpec() {
  beforeEach(function() {
    mailerErr = false;
    mailerTemplate = undefined;

    emailTemplatesErr = false;
    templateErr = false;
    templateHtml = undefined;
  });

  it('should send an email', function(done) {
    var req = new mockReq();
    var res = new mockRes(next);

    res.email = {
      templateName: 'template-name',
      to: 'test@test.com',
      subject: 'some subject',
      locals: {
        firstName: 'test'
      }
    };

    templateHtml = 'HTML';

    sut(req, res, next);

    function next(msg) {
      expect(msg).to.be.false;
      expect(mailerStubSpy.called).to.be.true;
      //expect(spy.calledWith(res.email.to, res.email.subject, templateHtml)).to.be.true;

      done();
    }
  });
});