在为以下代码S3resizer编写测试时,当我调用S3getStub时, stub testedModule似乎不起作用,我从mocha AssertionError: expected stub to have been called at least once, but it was never called获得了响应。

为什么S3resizer_get不 stub ?
s3Handler:

'use strict';

var s3 = new (require('aws-sdk')).S3();

var S3Handler = {};

S3Handler._get = function (bucketName, imgName, callback) {

    var params = {
        Bucket: bucketName,
        Key: imgName
    };

    s3.getObject(params, function (error, data) {
        if (error) {
            console.log("Error, %s", error);
            callback(error, null);
        }
        console.log("good data");
        callback(null, data);
    });
};

module.exports = S3Handler;
S3resizer:
/**
 * This function is called when the protocol defined in index.js is "s3:".
*/

'use strict';

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

var S3resizer = {};

S3resizer.rs = function (imgName, bucketName) {

S3get(bucketName, imgName, function (error, data) {console.log(data);});
};

module.exports = S3resizer;
tests:
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').noPreserveCache();
var url = require('url');
var mockDir = require('mock-fs');

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

before(function () {

    S3getStub = sinon.stub();

    rsStub = sinon.stub();

    readDirContStub = sinon.stub();

    readDirFileStub = sinon.stub();

    S3putStub = sinon.stub();

    sqsCreateStub = sinon.stub();

    sqsSendStub = sinon.stub();

    cbSpy = sinon.spy();

    callbSpy = sinon.spy();

    testedModule = proxyquire('../S3resizer', {
        './S3Handler': {
            _get: S3getStub,
            _put: S3putStub
        },
        './readDirectory': {
            _get: readDirFileStub,
            _getContent: readDirContStub
        },
        './resizer': {
            resize: rsStub
        },
        './sqsHandler': {
            _sendMessage: sqsSendStub
        },
    });

    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":"re-sized",
        "message": {
            "url":"S3://bucketname/images/908798",
            "sizes":["large","medium","small","thumbnail"]
        }
    };

    fakeResponse = {Body: 'image content'};

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



});

it("calls callback with message 'Done'", function () {
    S3getStub.callsArgWith(2, null, fakeResponse);

    testedModule.rs(imgName, bucketName);
    expect(S3getStub).has.been.called;
});

});

最佳答案

the fine manual:



您在告诉proxyquire捕获./S3Handler的require,实际上S3Resizer正在加载./S3Handler.js(包括.js)。那不完全一样。

09-25 17:48