Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core DynamodDB unit testing with XUnit

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?

like image 354
user1563677 Avatar asked Jul 27 '26 22:07

user1563677


1 Answers

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();
like image 66
Can Sahin Avatar answered Jul 29 '26 12:07

Can Sahin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!