node.js - sinon.stub 不 stub 原始方法

标签 node.js unit-testing sinon

在为我的以下代码 S3resizer 编写测试时,当我调用 testedModule 时, stub S3getStub 似乎无法正常工作,我得到来自 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;

测试:

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 :

therefore specify it exactly as in the require statement inside the tested file

你告诉 proxyquire 去捕捉 ./S3Handler 的需求,实际上 S3Resizer 正在加载 ./S3Handler.js(包括.js)。这并不完全相同。

关于node.js - sinon.stub 不 stub 原始方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593632/

相关文章:

javascript - Angular http.get 未从 mysql nodejs 接收数据

javascript - AWS Lambda,计费时间比处理时间长

python - 使用 python 进行 TDD,意外的单元测试错误

javascript - 我如何断言一个函数有 `await` 使用 should.js 编辑了一个异步函数

Javascript 测试 stub 全局变量函数

javascript - 如何在 Chai 中模拟或 stub 'instanceof' |诗农 |摩卡

node.js - 系统仅允许一名用户登录

javascript - 如果任何端口 9229 有什么意义,可以更改吗?

python - py.test main() 调用示例

unit-testing - 使用可绑定(bind)和依赖项测试 aurelia customElement