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

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

问题描述

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

Using C#, .net 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,但使用.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 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的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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