gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:12:33 26 4
gpt4 key购买 nike

在为我的以下代码 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com