C# Unit Test case Moq dynamodb context object for dynamoDB operation

Viewed 31

I'm trying to Moq dynamodb context, here the context is constructed on the constructor so I'm trying to send the mock object, when I hover, it's mocked but in the end, it fails like "parameter value cannot be null"(AWSSigner), please help me to write the test case incorrect way.

 public interface IDbContext<T> where T : class
        {
               Task SaveEntityAsync(T entity, DynamoDBOperationConfig dynamoDBOperationConfig = null);
        }
    
      public class DbContext<T> : IDbContext<T> where T : class
        {
             private readonly Amazon.DynamoDBv2.DataModel.IDynamoDBContext context;
             public DbContext(IFactory factory)
            {
                    context = factory.GetDynamoDBContext();
            }   
    
            public async Task SaveEntityAsync(T entity, DynamoDBOperationConfig 
            dynamoDBOperationConfig = null)
            {
                await context.SaveAsync(entity,dynamoDBOperationConfig );// Fails on this place
            }
    }

 public class DynamoDBFactory : IFactory 
    {
        private readonly AmazonDynamoDBClient amazonDynamoDBClient;

        public DynamoDBFactory()
        {
            amazonDynamoDBClient = new AmazonDynamoDBClient();
        }

        public DynamoDBContext GetDynamoDBContext()
        {
            if (_config == null)
            {
                return new DynamoDBContext(amazonDynamoDBClient);
            }
        }
}

     public interface IFactory 
        {
            DynamoDBContext GetDynamoDBContext();
    
        }
    
    
        // Unit test case
        public class RepoTestCase
        {
               private readonly Mock<IFactory> _factory;
    private readonly Mock<DynamoDBContext> _dynamoDBContext;
            public RepoTestCase()
            {
                 _dynamoDBContext = new Mock<DynamoDBContext>();
                _dynamoDBFactory = new Mock<IFactory>();
                    _dynamoDBFactory.Setup(x => 
                 x.GetDynamoDBContext()).Returns(_dynamoDBContext.Object);
            }
        
        public async Task SaveAsyncTest()
       {
        DbContext<Address> context = new DbContext<Address>();
        await context.SaveEntityAsync(addressrequest);  // Fails
        // Assert
        }
     }
0 Answers
Related