0

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?

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
Roman A.
  • 686
  • 5
  • 25

1 Answers1

1

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);
        });
    });
});
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
MrB
  • 1,544
  • 3
  • 18
  • 30
  • Hi! Could you provide your email to connection? – Roman A. Feb 15 '16 at 19:36
  • @Roman sorry I don't feel comfortable giving out my email. To be able to help I think all you need to add to the your example code is what your requiring and the methods your calling and I think I could help you further. – MrB Feb 16 '16 at 14:55
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11354203) – Toby Speight Feb 22 '16 at 17:26
  • @TobySpeight its a bit of code should I include the test file and the index file in the answer? It obviously wouldn't be able to run without the rest of the code. – MrB Feb 22 '16 at 21:29