lundi 26 décembre 2016

How to mock AWS S3.getobject() using mocha?

During tests using mocha, i'm having invocation issue with S3.getObject().

My code in Lambda is like this:

index.js:

S3.getObject(s3FileParams, function(err, data) {
    if (err) {
        var message = "Error while trying to get file object " + fullFileName + " from bucket " + bucketName + ". Make sure they exist and your bucket is in the same region as this function. Error: " + err;
        console.error(message);
    }
    else {
        userMetaDataJson = data.Metadata;
    }
    resolve();
})

And in the test file index.test.js (using mocha)

AWSMock.mock('S3', 'getObject', function (params, callback){
    var metaDataParams = {
        "Metadata": {
            "startDate": "2016-11-11 12:34:56:000",
            "endDate":   "2016-11-11 12:34:56:000",
            "userName":  "userName",
            "originalFileName": "originalFileName"
        }
    };
    callback(false, metaDataParams);
});

When running this test with mocha, there is no call to the mocked getObject() and it's calling to the original one, is there anything that I can do ?

Is there any other approach I could use?

Aucun commentaire:

Enregistrer un commentaire