mardi 2 juin 2015

sinon.stub not being called

When writing the tests for my following code, S3resizer, the first stub S3getStub is not being called when I call testedModule. However if I call the stub manually, the expectation is met.

Why is this happening?

S3resizer:

/**
 * This function is called when the protocol defined in index.js is "s3:".
 * @async.series - is used when we want to call a list of functions in a series, the next being called only once the previous has finished running.
 * @async.waterfall - is used like 'async.series' but passed the results form the first function to the next second.
 * @async.eachSeries - applies the function to each item in the array in series. the iterator functions will end in order.
 * @fs type function - This function calls a list of functions using the methods 'series', 'waterfall', 'eachSeries' and 'series' from the 'async' module.
 * @params imgName type string - the original name of the file to be manipulated.
 * @params bucketName type string - the name of the S3 bucket that will be used.
 * @params sizesObj type array of objects - the object containing the list of sizes we wish to resize our object too.
 * @params imageType type string - the type of image.
 * @params obj type object - the object being sent in the sqs message to the sqs queue.
 * @var dir - is set to '/tmp/' as this is the path to the temporary directory on aws lambda where this function will run.
 */

'use strict';

var async = require('async');
var S3get = require("./S3Handler.js")._get;
var S3put = require("./S3Handler.js")._put;
var readDirFile = require("./readDirectory.js")._get;
var readDirCont = require("./readDirectory.js")._getContent;
var rs = require("./resizer.js").resize;
var sqsSend = require("./sqsHandler.js")._sendMessage;

var S3resizer = {};

S3resizer.rs = function (imgName, bucketName, sizesObj, imageType, obj, cb) {

    var dir = "/tmp/";

    async.series([
        function (next) {

            async.waterfall([
                function getData (asyncCallback) {
                    S3get(bucketName, imgName, asyncCallback);
                },
                function resizeImg (data, asyncCallback) {

                    async.eachSeries(sizesObj, function (item, mapNext) {
                        rs(data, imgName, dir, item, mapNext);
                    }, function (err) {
                        if (err) {
                            asyncCallback(err);
                        } else {
                            asyncCallback(null);
                        }
                    });
                }
            ], function (err, result) {
                if(err) {
                    next(err);
                } else {
                    next(null, result);
                }
            });
        },

        function (next) {
            async.waterfall([

                function readDir (asyncCallback) {
                    readDirFile(dir, asyncCallback);
                },
                function readFiles (files, asyncCallback) {

                    async.each(files, function (file, mapNext) {
                        readDirCont(file, dir, function (data) {
                            S3put(bucketName, data, file, imgName, imageType, mapNext);
                        });
                    }, function (err) {
                        if (err) {
                            asyncCallback(err);
                        } else {
                            asyncCallback();
                        }
                    });
                },
                function sendSqs (asyncCallback) {
                    sqsSend(obj, asyncCallback);
                }
            ], function (err) {
                if(err) {
                    next(err);
                } else {
                    next();
                }
            });
        }
    ], function (err, results) {
        if (err) {
            cb(err);
        } else {
            cb(null, results);
        }
    });
};

module.exports = S3resizer;

tests:

require('blanket')({
    pattern: function (filename) {
        return !/node_modules/.test(filename);
    }
});

// in terminal, type the following command to get code coverage: mocha -R html-cov > coverage.html

var chai = require('chai');
var sinonChai = require("sinon-chai");
var expect = chai.expect;
var extend = require('lodash').extend;
var sinon = require('sinon');
chai.use(sinonChai);
var proxyquire = require('proxyquire');
var url = require('url');
var mockDir = require('mock-fs');

describe("S3resizer", function () {
    var testedModule, _S3, readDir, resizer, sqs, fakeResponse, fakeFiles, S3getStub, rsStub, readDirFileStub, readDirContStub, S3putStub, sqsSendStub, cbSpy, callbSpy, imgName, bucketName, sizesObj, imageType, obj;

    before(function () {

        _S3 = require("../S3Handler.js");
        readDir = require("../readDirectory.js");
        resizer = require("../resizer.js");
        sqs = require("../sqsHandler.js");
        testedModule = require('../S3resizer.js');

        imgName = "Whatever";

        bucketName = "Chappie";

        sizesObj = [
            { width: 800, height: 800, name: 'large' },
            { width: 500, height: 500, name: 'medium' },
            { width: 200, height: 200, name: 'small' },
            { width: 45, height: 45, name: 'thumbnail'}
        ];

        imageType = "png";

        obj = {
            "event":"resized",
            "message": {
                "url":"S3://bucketname/images/Whatever.png",
                "sizes":["large","medium","small","thumbnail"]
            }
        };

        fakeResponse = { Body: new Buffer([1,2,3,4]) };

        fakeFiles = ["thumbnail_Whatever", "small_Whatever", "medium_Whatever", "large_Whatever"];

        S3getStub = sinon.stub(_S3, "_get");

        rsStub = sinon.stub(resizer, "resize");

        readDirContStub = sinon.stub(readDir, "_getContent");

        readDirFileStub = sinon.stub(readDir, "_get");

        S3putStub = sinon.stub(_S3, "_put");

        sqsSendStub = sinon.stub(sqs, "_sendMessage");

        cbSpy = sinon.spy();

        callbSpy = sinon.spy();

        mockDir({
            "tmp" : {
            }
        });
    });

    after(function () {

        S3getStub.restore();

        rsStub.restore();

        readDirContStub.restore();

        readDirFileStub.restore();

        S3putStub.restore();

        sqsSendStub.restore();

        mockDir.restore();
    });

    it("calls callback with message 'Done'", function (done) {

        S3getStub.callsArgWith(2, null, fakeResponse);


        testedModule.rs(imgName, bucketName, sizesObj, imageType, obj, function () {
            cbSpy.apply(null, arguments);
            expect(S3getStub).has.been.called;
            done();
        });
    });
});

Aucun commentaire:

Enregistrer un commentaire