.Net Core DynamodDB 单元测试与 XUnit [英] .Net Core DynamodDB unit testing with XUnit

查看:22
本文介绍了.Net Core DynamodDB 单元测试与 XUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using C#, .net core 2.0, dynamo db

我有我的 web api,它与我的 dynamo db 数据库交互,具有 Get 和 Post 方法.

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

方法示例:

    [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 但使用 .net core 2.0 我相信我们必须使用 XUnit:https://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 .net core 2.0 I believe we have to use XUnit: https://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 部分不可测试.任何人都可以分享 xunit 测试的示例,我们可以在其中测试参数并查看预期结果吗?

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.但是尝试使用dependecy injection-ish来做到这一点.好多了.

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();

然后用这个mock来初始化MyDynamodbHandler类

Then use this mock to initiate MyDynamodbHandler class

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

这篇关于.Net Core DynamodDB 单元测试与 XUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆