sinon.stub不是原始方法的存根

时间:2015-06-02 10:33:20

标签: node.js unit-testing sinon

在为我的以下代码S3resizer编写测试时,当我调用S3getStub时,存根testedModule似乎无效,我从mocha AssertionError: expected stub to have been called at least once, but it was never called得到回复。

为什么S3resizer_get没有被删除?

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;  
});

});

2 个答案:

答案 0 :(得分:3)

来自the fine manual

  因此,

完全按照里面的require语句指定   测试文件

您告诉proxyquire要抓住./S3Handler的要求,实际上S3Resizer正在加载./S3Handler.js(包括.js)。这不完全一样。

答案 1 :(得分:1)

这很有用。我不得不新近要求测试中的模块,以避免使用缓存版本并重构代码以使用require模块的全局覆盖。

describe("S3resizer succesfull call", function () {

    // To avoid having to refactor code with a global override of the require method and using the cached versions from previous tests, freshly require the modules and inject in stubs.
    var S3 = require("../S3Handler");
    var read = require("../readDirectory");
    var _resizer= require("../resizer");
    var _sqs = require("../sqsHandler");

    var testedModule, fakeResponse, fakePutMessage, fakeSqsMessage, fakeFiles, S3getStub, rsStub, readDirFileStub, readDirContStub, S3putStub, sqsSendStub, cbSpy, callbSpy, imgName, bucketName, sizesObj, imageType, obj;

    before(function () {

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

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

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

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

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

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

        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'};

        fakePutMessage = {messageId: "1223abc"};

        fakeSqsMessage = {BinaryValue: "123"};

        fakeFiles = ["thumbnail-Whatever", "small-Whatever", "medium-Whatever", "large-Whatever"];

        S3getStub.callsArgWith(2, null, fakeResponse);

        rsStub.callsArgWith(4, null);

        readDirFileStub.callsArgWith(1, null, fakeFiles);

        readDirContStub.callsArgWith(2, null, fakeResponse);

        S3putStub.callsArgWith(5, null, fakePutMessage);

        sqsSendStub.callsArgWith(1, null, fakeSqsMessage);

        testedModule.rs(imgName, bucketName, sizesObj, imageType, obj, function () {
            cbSpy.apply(null, arguments);
        });
    });

    after(function () {
        S3._get.restore();
        _resizer.resize.restore();
        read._getContent.restore();
        read._get.restore();
        S3._put.restore();
        _sqs._sendMessage.restore();
    });
};