简体   繁体   English

如何模拟 DynamodbContext BatchWrite 以在网络中进行单元测试

[英]How to mock DynamodbContext BatchWrite for unit testing in net

I have a method that adds Items in the DynamoDb table using DynamoDbContext.我有一个使用 DynamoDbContext 在 DynamoDb 表中添加项目的方法。

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.由于 BatchWrite 没有任何构造函数,而且它的属性也不是虚拟的。 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?也许通过 mocking BatchWrite?

Have you considered using DynamoDB local to run tests?您是否考虑过使用 DynamoDB local 来运行测试? 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/ ).我为它创建了一个助手 package ( https://www.nuget.org/packages/Wadsworth.DynamoDB.Test It just manages spinning up the DynamoDB local in a Docker container.它只是管理在 Docker 容器中启动本地 DynamoDB。 You can see the source for ithere , which includes an example.您可以在此处查看它的源代码,其中包括一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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