I have the file upload POST point in my Hapi.Js server. Here is code:
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);
});
}
}
}]);
How correctly cover the case "should return the uploaded image path" for this code/point without hitting DB and AWS?
I think you might need a package such as proxyquire, to help you mock out methods and make them return valid results so your logic can continue.
Example usage (from 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);
});
});
});