.Net Core DynamodDB与XUnit的单元测试

编程入门 行业动态 更新时间:2024-10-23 11:16:39
本文介绍了.Net Core DynamodDB与XUnit的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用C#、. net core 2.0,dynamo db

Using C#, core 2.0, dynamo db

我有自己的Web api,可与同时具有Get和Post方法的dynamo db数据库交互。

I have my web api, that interact with my dynamo db database having both Get and Post methods.

Mehthod示例:

Example of Mehthod:

[HttpGet("api/data")] public async Task<List<string>> GetAllData(string userId, string type, string status) { var creds = new BasicAWSCredentials(awsId, awsPassword); var dynamoClient = new AmazonDynamoDBClient(creds, dynamoRegion); var context = new DynamoDBContext(dynamoClient); List<ScanCondition> conditions = new List<ScanCondition>(); conditions.Add(new ScanCondition("UserId", ScanOperator.Equal, userId)); conditions.Add(new ScanCondition("Type", ScanOperator.Equal, type)); conditions.Add(new ScanCondition("Status", ScanOperator.Equal, status)); var results = await context.ScanAsync<Common.Job>(conditions, new DynamoDBOperationConfig() { OverrideTableName = MyDynamoTable }).GetRemainingAsync(); return results.Select(x => x.UpdatedBy.ToLower()).ToList(); }

现在,我想为我的api方法编写单元/集成测试。之前我使用过NUnit,但使用 core 2.0,我相信我们必须使用XUnit: xunit.github.io/docs/getting-started-dotnet-core

Now I want to write unit/integration tests for my api methods. Earlier I had used NUnit but with core 2.0 I believe we have to use XUnit: xunit.github.io/docs/getting-started-dotnet-core

在我的项目中设置Xunit应该不是问题

Setting up Xunit in my project should not be an issue.

我想知道如何在此处编写涉及dynamo db的测试。这是我第一次在这里使用任何AWS服务。

I wanted to know how can I write test which involve dynamo db here. This is the first time I am using any AWS service here.

因此,基本上我需要知道如何模拟aws连接,dynamo db,然后使用各种参数如上面的方法所示。

So bascially I need to know how can I mock up a aws connection, dynamo db and then use various params as shown in my method above.

我找不到有关此主题的详细信息或任何较早的有用帖子,因此在此处发布一个。

I could not find much details or any earlier helpful post on this topic so posting one here.

如果aws dynamo db part无法测试。任何人都可以分享xunit test的示例,我们可以在其中测试参数并查看预期结果吗?

If aws dynamo db part is not testable. Can anyone share the example of xunit test where we can test the params may be and see the expected result?

推荐答案

AWS SDK可以正常工作与接口。您可以轻松模拟 IAmazonDynamoDB 界面。但是,请尝试使用可靠的注入式操作。好多了。

AWS SDK work with interfaces. You can mock interface IAmazonDynamoDB easily. But try to do it with dependecy injection-ish. Much better.

类似

private readonly IAmazonDynamoDB dynamodbClient; private readonly IDynamoDBContext context; public MyDynamodbHandler(IAmazonDynamoDB client) { this.dynamodbClient = client; this.context = new DynamoDBContext(client); } [HttpGet("api/data")] public async Task<List<string>> GetAllData(string userId, string type, string status) { List<ScanCondition> conditions = new List<ScanCondition>(); conditions.Add(new ScanCondition("UserId", ScanOperator.Equal, userId)); conditions.Add(new ScanCondition("Type", ScanOperator.Equal, type)); conditions.Add(new ScanCondition("Status", ScanOperator.Equal, status)); var results = await this.context.ScanAsync<Common.Job>(conditions, new DynamoDBOperationConfig() { OverrideTableName = MyDynamoTable }).GetRemainingAsync(); return results.Select(x => x.UpdatedBy.ToLower()).ToList(); }

因此每个函数都使用注入的 IAmazonDynamoDB 。您要做的就是在开头模拟该实例

So every function uses the injected IAmazonDynamoDB. All you have to do is to mock this instance at the beginning

例如 dynamodbClientMock = new Mock();

Such as dynamodbClientMock = new Mock();

然后使用该模拟程序来初始化MyDynamodbHandler类

Then use this mock to initiate MyDynamodbHandler class

var dynamodbHandler = new MyDynamodbHandler(dynamodbClientMock); dynamodbHandler.GetAllData();

更多推荐

.Net Core DynamodDB与XUnit的单元测试

本文发布于:2023-11-08 14:09:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569589.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   Core   Net   XUnit   DynamodDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!