我的Hapi.Js服务器上有文件上传POST点。 这是代码:
server.route([{
method: 'PUT',
path: '/upload/{id}',
config: {
handler: function(req,res) {
async.waterfall([
function checkEntityInDbExists(req.params.id,callback) {
...
callback(null, entityId);
},
function uploadPictureToAWS(entityId, callback) {
...
callback(null, imageLink);
},
function savePictureLinkInDbEntity(entityId, callback) {
...
callback(null, imageLink);
}
], function(err, result) {
if (err) {
return reply(err);
}
return reply(result);
});
}
}
}]);
如何正确覆盖案例"应该返回上传的图片路径"对于这个代码/点没有命中DB和AWS?
答案 0 :(得分:1)
我认为你可能需要一个像proxyquire这样的软件包来帮助你模拟方法并让它们返回有效的结果,这样你的逻辑就可以继续了。
示例用法(来自Async-Hapi-Test-Example):
var assert = require("assert");
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
var proxyquire = require("proxyquire").noCallThru();
var expect = chai.expect;
chai.should();
chai.use(sinonChai);
describe("Testing route index", function() {
var sut;
var db;
var aws;
beforeEach(function() {
db = {
check: sinon.spy(),
savePic: sinon.spy(function(){ return "a link?"; })
}
aws = {
upload: sinon.spy()
}
sut = proxyquire('./index', {"./db": db, "./aws": aws});
});
describe("upload", function() {
it("should pass", function(done){
var request = {
params: {
id: 9001
}
}
var reply = function(results) {
results.should.equal('a link?');
db.check.should.been.calledOnce;
db.savePic.should.been.calledOnce;
aws.upload.should.been.calledOnce;
done();
}
sut[0].config.handler(request, reply);
});
});
});