Node.js mocha测试将'test'作为变量发送到节点应用程序

Node.js mocha测试将'test'作为变量发送到节点应用程序,node.js,unit-testing,Node.js,Unit Testing,在为我的输入文件编写测试时,index.js我遇到了一个问题,即命令mocha test将test作为参数传递给index.js,因为它使用process.argv接收要在开发环境中运行的参数。我曾认为,通过使用minimist来命名参数可以解决这个问题,但是在运行测试时,这个问题仍然存在。这样,我的测试就不会使用测试套件中提供的对象,如下面的代码所示 如何避免这种情况,以便在运行测试时,它使用我在测试设置中提供的事件对象,而不是命令mocha test index.js 'use strict

在为我的输入文件编写测试时,
index.js
我遇到了一个问题,即命令
mocha test
test
作为参数传递给
index.js
,因为它使用
process.argv
接收要在开发环境中运行的参数。我曾认为,通过使用
minimist
来命名参数可以解决这个问题,但是在运行测试时,这个问题仍然存在。这样,我的测试就不会使用测试套件中提供的对象,如下面的代码所示

如何避免这种情况,以便在运行测试时,它使用我在测试设置中提供的
事件
对象,而不是命令
mocha test

index.js

'use strict';


var _ = require("underscore");
var async = require('async');
var argv = require("minimist")(process.argv.slice(2));

var getprotocol = require("./getProtocol");
var _getprotocol = getprotocol.getProtocol;

var S3rs = require("./S3resizer");
var s3resizer = S3rs.rs;

var objCr = require("./objectCreator");
var createObj = objCr.creator;

var fileRs = require("./fileResizer");
var fileResizer = fileRs.rs;

var configs = require("./configs.json");

var mkDir = require("./makeDir");
var makeDir = mkDir.handler;



exports.imageRs = function (event, context) {

    var _path = argv.x || event.path; //argv.x used to be process.argv[2]

    console.log("Path, %s", _path);

    var _dir = argv.y; // used to be process.argv[3]

    console.log(_dir);

    var parts = _getprotocol(_path);

    var imgName = parts.pathname.split("/").pop();

    console.log("imgName: %s", imgName);

    var s3Bucket = parts.hostname;

    var s3Key = imgName;

    var _protocol = parts.protocol;

    console.log(_protocol);

    // RegExp to check for image type
    var imageTypeRegExp = /(?:(jpg)|(png)|(jpeg))$/;

    var sizesConfigs = configs.sizes;

    var obj = createObj(_path);

    // Check if file has a supported image extension
    var imgExt = imageTypeRegExp.exec(s3Key);

    if (imgExt === null) {
        console.error('unable to infer the image type for key %s', s3Key);
        context.done(new Error('unable to infer the image type for key %s' + s3Key));
        return;
    }

    var imageType = imgExt[1] || imgExt[2];
    // Do more stuff here
};

if (!process.env.LAMBDA_TASK_ROOT) {
    exports.imageRs();
}
test.js

describe("imgeRs", function () {

    var getprotocol = require("../getProtocol");
    var S3rs = require("../S3resizer");
    var objCr = require("../objectCreator");
    var mkDir = require("../makeDir");
    var fileResizer = require("../fileResizer");

    describe("Calling S3", function () {
        describe("Success call", function () {

            var testedModule, eventObj, contextDoneSpy, S3resizerStub, objCreatorStub, getProtocolStub, fakeResults, mkDirStub, fileResizerStub;

            before(function (done) {
                contextDoneSpy = sinon.spy();

                S3resizerStub = sinon.stub(S3rs, "rs");

                objCreatorStub = sinon.stub(objCr, 'creator');

                getProtocolStub = sinon.stub(getprotocol, "getProtocol");

                mkDirStub = sinon.stub(mkDir, "handler");

                fileResizerStub = sinon.stub(fileResizer, "rs");

                eventObj = {"path": "s3://theBucket/image.jpeg"};

                fakeResults = ["resized"];

                testedModule = proxyquire("../index", {
                    './getProtocol': {
                        'getProtocol': getProtocolStub
                    },
                    './S3resizer': {
                        'rs': S3resizerStub
                    },
                    './objectCreator': {
                        'creator': objCreatorStub
                    },
                    './makeDir': {
                        'handler': mkDirStub
                    },
                    './fileResizer': {
                        'rs': fileResizerStub
                    }
                });

                S3resizerStub.callsArgWith(5, null, fakeResults);

                testedModule.imageRs(eventObj, {done: function (error) {
                    contextDoneSpy.apply(null, arguments);
                    done();
                }});
            });

            after(function () {
                S3rs.rs.restore();
                objCr.creator.restore();
                getprotocol.getProtocol.restore();
                mkDir.handler.restore();
                fileResizer.rs.restore();
            });

            it("calls context.done with no error", function () {
                expect(contextDoneSpy).has.been.called;
            });
        });
    });
});

能否将imageRs更改为始终使用“event”对象,并在index.js的末尾传递它argv.x?还是反过来?(如果提供,请使用event.path,如果没有传递事件,请使用argv.x?)我已将条件更改为这样:`if(!event){console.log(“使用argv.x”);_path=argv.x;}否则{console.log(“使用事件”);_path=event.path;}`但结果仍然相同。它仍然尝试使用
argv.x
。和
console.log(event)
返回未定义的条件。