我正在使用AWS lambda中的节点处理程序,我需要使用该函数进行集成测试的另一个文件,但我不能用sinon或mockery模拟传输器。
index.js函数:
var nodemailer = require('nodemailer');
exports.handler = (event, context, callback) =>
{
var transporter=createTransporter();
transporter.sendMail(data, function (error, success) {
console.log(error);
response = getResponse(404, error);
}
callback(null, response);
});
}
function createTransporter() {
return nodemailer.createTransport({
service: "SMTP",
auth: {
user: "XXXX@XXX",
pass: "XXXX"
}
});
}
目的是模拟函数createTransporter(),以便在使用mocha和期望的javascript文件测试中调用它时不发送任何电子邮件:
var mockery = require('mockery');
var nodemailerMock = require('nodemailer-mock');
var index = require("../index.js");
describe("The handler function tests", function () {
before(function () {
mockery.enable({
warnOnUnregistered: false
});
mockery.registerMock('nodemailer', nodemailerMock);
});
it('JSON error html ', function () {
var callback = function (name, response) {
expect(JSON.stringify(response.statusCode)).to.be('404');
};
var context = {};
index.handler(event, context, callback);
});
});
答案 0 :(得分:0)
我不是百分之百确定为什么会失败,但我建议两件事之一:
dismissAction
...这里可能存在细微差别mainFocusTextField.becomeFirstResponder()
,以便您可以为其分配新值,无论是否为模拟。这不是“保持实施细节私密”,它确实有用答案 1 :(得分:0)
我写了nodemailer-mock
:)
您遇到的问题是,在通过var index = require("../index.js");
模拟nodemailer
之前,您正在调用mockery
,因此它已经在模块缓存中。我在自述文件的示例中包含// Make sure anything that uses nodemailer is loaded here, after it is mocked...
,但应该更清楚。
在require("../index.js")
被模拟后移动nodemailer
,它将按预期工作。
var mockery = require('mockery');
var nodemailerMock = require('nodemailer-mock');
// don't require here since you will get the real nodemailer and cache it
var index;
describe("The handler function tests", function () {
before(function () {
mockery.enable({
warnOnUnregistered: false
});
mockery.registerMock('nodemailer', nodemailerMock);
// do the require() here after nodemailer is mocked
index = require("../index.js");
});
// your tests here should now use nodemailer-mock
it('JSON error html ', function () {
var callback = function (name, response) {
expect(JSON.stringify(response.statusCode)).to.be('404');
};
var context = {};
index.handler(event, context, callback);
});
});
另一种选择是使用{ useCleanCache: true }
选项调用mockery.resetCache();
,尽管我的结果好坏参半。请参阅嘲弄文档中的Controlling the Module Cache。