0
我有一個測試是在一個殘缺的方法上進行間諜活動。該方法僞造一個依賴項。我可以驗證方法實際上是否被調用。我甚至可以通過預期的參數驗證它是否被調用。但是,在查看called
或calledWith
時,間諜未提供true
。sinon.spy(func)即使在調用時也不會產生調用
這是代理mailer
依賴關係的mailerStub
方法。
我甚至已經爲spy.displayName
和它的mailer
放置console.log
預期。
我在這裏做錯了什麼?
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: '[email protected]',
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();
}
});
});