https
を使用してレスポンスURLにデータを送信するテスト対象モジュールがあります。これを行う前に、AWS SDKを呼び出します。 AWS SDKがhttps
を使用して呼び出しをスタブしたくないのですが、テスト中のモジュールが使用するhttps.postへの呼び出しをスタブする必要があります(問題がある場合はAWS Lambdaユニットテストです)。Sinonテスト中のモジュールのみのNodeJSスタブ
は、私はこれを実現するにはどうすればよい
var aws = require("aws-sdk");
var promise = require("promise");
exports.handler = function (event, context) {
var iam = new aws.IAM();
promise.denodeify(iam.getUser.bind(iam))().then(function (result) {
....
sendResponse(...);
}, function (err) {
...
});
};
// I only want to stub the use of https in THIS function, not the use of https by the AWS SDK itself
function sendResponse(event, context, responseStatus, responseData) {
var https = require("https");
var url = require("url");
var parsedUrl = url.parse(event.ResponseURL);
var options = {
...
};
var request = https.request(options, function (response) {
...
context.done();
});
request.on("error", function (error) {
...
context.done();
});
// write data to request body
request.write(...);
request.end();
}
次のテストコードテスト(app.js)下
describe('app', function() {
beforeEach(function() {
this.handler = require('../app').handler;
this.request = sinon.stub(https, 'request');
});
afterEach(function() {
https.request.restore();
});
describe('#handler()', function() {
it('should do something', function (done) {
var request = new PassThrough();
var write = sinon.spy(request, 'write');
this.request.returns(request);
var event = {...};
var context = {
done: function() {
assert(write.withArgs({...}).calledOnce);
done();
}
}
this.handler(event, context);
});
});
});
そして、私のモジュールを考えてみましょうか?