0

我有一个测试正在监视一个存根的方法。该方法正在伪造依赖项。我可以验证该方法实际上正在被调用。我什至可以验证它是用预期的参数调用的。true但是,间谍在查看called或时没有提供calledWith

mailerStub是代理mailer依赖项的方法。

我什至console.log为 the放置了一个spy.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();
    }
  });
});
4

1 回答 1

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();
    }
  });
});
于 2015-11-13T23:17:07.973 回答