How to unit test QueryAsync function in IDynamoDBContext using NSubstitute?

23 Views Asked by At

I'm using IDynamoDBContext interface to perform a QueryAsync operation in my dynamodb GSI. I'm using NSubstitue for unit testing. When writing a test case for my QueryAsync function I'm facing issue. Actually this is how my code looks,

var expectedItems = new List<UserDocument>
{
new UserDocument { Id = "1", Name = "User1" },
new UserDocument { Id = "2", Name = "User2" }
};
var indexName = "GSIName";
var partitionKey = "PartitionKey";
var partitionKeyValue = "PartitionKeyValue";
var dbContext = Substitute.For<IDynamoDBContext>();
var asyncSearch = Substitute.For<AsyncSearch<UserDocument>>();
asyncSearch.GetRemainingAsync().Returns(expectedItems);
dbContext.QueryAsync<UserDocument>(indexName, partitionKey, 
QueryOperator.Equal,Arg.Any<List<object>>()).Returns(asyncSearch);
var dynamoDBService = new DynamoDBService(dbContext);
// Act
var result = await dynamoDBService.QueryAsync(indexName, partitionKey, QueryOperator.Equal, 
new List<object> { partitionKeyValue });
// Assert
Assert.AreEqual(expectedItems, result);

when I run the test, I'm facing the following issue, "System.NotSupportedException : Parent does not have a default constructor. The default constructor must be explicitly defined."`

0

There are 0 best solutions below