.Net Core DynamodDB unit testing with XUnit

≯℡__Kan透↙ 提交于 2019-12-11 00:46:31

问题


Using C#, .net core 2.0, dynamo db

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

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

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

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

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.

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.


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?


回答1:


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

Something like

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

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

Such as dynamodbClientMock = new Mock();

Then use this mock to initiate MyDynamodbHandler class

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


来源:https://stackoverflow.com/questions/52784546/net-core-dynamoddb-unit-testing-with-xunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!