简体   繁体   中英

How to mock DynamodbContext BatchWrite for unit testing in net

I have a method that adds Items in the DynamoDb table using DynamoDbContext.

var batchWriteObj = context.CreateBatchWrite<myDynamoDBModel>
                                            (new DynamoDBOperationConfig { TableNamePrefix = "abc" });
                    batchWriteObj.AddPutItem(myDynamoDBModel);

I want to test this piece in my Unit test. Since BatchWrite does not have any constructor, and also its properties are not virtual. I am unable to create a mock for it.

Is there any way I can test this piece of code? maybe by mocking the BatchWrite?

Have you considered using DynamoDB local to run tests? It has the added advantage of validating that your calls are doing what you expect them to do. I created a helper package for it a while back ( https://www.nuget.org/packages/Wadsworth.DynamoDB.TestUtilities/ ). It just manages spinning up the DynamoDB local in a Docker container. You can see the source for ithere , which includes an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM