aws-sdk-mock icon indicating copy to clipboard operation
aws-sdk-mock copied to clipboard

How do I mock dynamoDB.DocumentClient batchWrite? "Requested resource not found"

Open peter-borgstedt opened this issue 7 years ago • 5 comments

This is my pattern on all, works for 'get' 'put' 'delete' and so on. But when I try to mock batchWrite it doesn't find my mockup. Have also tried 'batchWriteItem' does not work.

AWS.mock('DynamoDB.DocumentClient', 'batchWrite', (params: any, callback: any) => callback(null, {}));

Anyone know how?

peter-borgstedt avatar May 29 '18 21:05 peter-borgstedt

The method you're looking for is probably batchWriteItem. batchWrite doesn't seem to be a method exposed by AWS SDK.

kevinrambaud avatar Jun 11 '18 13:06 kevinrambaud

The method you're looking for is probably batchWriteItem. batchWrite doesn't seem to be a method exposed by AWS SDK.

No, for DocumentClient it is really called batchWrite: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#batchWrite-property

tankist avatar Feb 14 '19 12:02 tankist

Was there ever an answer for this? I am encountering the same issue. In my case I got past the error by providing params that validly represent a real dynamoDB table. Then it was clear that the mock was not working because the non-mocked PutItem actually completed and wrote the item to the specified table.

Here is my code: `describe('Tweet handling', function() { describe('Handle tweet with reference', function() { it('should write a single request', function() { var AWS = require("aws-sdk");

  awsMock.mock('DynamoDB.DocumentClient', 'batchWrite', function(params, callback){
    callback(null, 'test');
  });

  var docClient = new AWS.DynamoDB.DocumentClient();

  var batchWriteJSON = 
  {
    RequestItems: { 
      'TestTable': [
        {
          PutRequest: {
            Item: {
              id: '1',
              a: 'b',
              b: 'c'
            }
          }
        }
      ]
    }
  };
  
  docClient.batchWrite(batchWriteJSON, function(err, data){
    assert.equal(data, 'test');

    awsMock.restore('DynamoDB.DocumentClient', 'batchWrite');
  });      
});

}); }); `

When I created a TestTable, the test created the item with the specified properties.

GlenBikes avatar Nov 17 '19 03:11 GlenBikes

Is there any plans to mock batchWrite on DynamoDB.DocumentClient

zkrige avatar Nov 25 '19 11:11 zkrige

from the aws docs:

Puts or deletes multiple items in one or more tables by delegating to AWS.DynamoDB.batchWriteItem().

so @kevinrambaud was right, i was able to mock using a sinon stub doing this:

const dynamoBatchWriteStub = sinon.stub().resolves(WHATEVER)
AWS.mock('DynamoDB', 'batchWriteItem', dynamoBatchWriteStub)

fwiw, this will only apply to dynamoDb.DocumentClient.batchWrite().promise()

chinitotuchman avatar Jul 29 '20 18:07 chinitotuchman