Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a mocked IAmazonDynamoDB instance in DynamoDBContext #1801

Closed
kpenergy opened this issue Feb 19, 2021 · 3 comments
Closed

Using a mocked IAmazonDynamoDB instance in DynamoDBContext #1801

kpenergy opened this issue Feb 19, 2021 · 3 comments
Labels
closed-for-staleness dynamodb guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@kpenergy
Copy link

kpenergy commented Feb 19, 2021

The Question

Is there any way to inject a mocked IAmazonDynamoDB instance into AmazonDynamoDB , in order to catch the request that hits the mocked client so the request details can be verified?

I'm trying to do so by injecting a mocked IAmazonDynamoDB instance into DynamoDBContext, constructing a new instance of my class to be passed into the DynamoDBContext, and calling SaveAsync, but I'm hitting a NullReferenceException.

[Fact]
public void Test()
{
	var clientMock = new Mock<IAmazonDynamoDB>();

	var context = new DynamoDBContext(clientMock.Object);

	Dictionary<string, AttributeValue> attributes = null;

	clientMock
		.Setup(m => m.PutItemAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, AttributeValue>>(), default))
		.Callback((string s, Dictionary<string, AttributeValue> a, CancellationToken c) => attributes = a);

	var model = new MyModel { HashKey = Guid.NewGuid().ToString() };

	context.SaveAsync(model).Wait();
}

public class MyModel
{
	[DynamoDBHashKey]
	public string HashKey { get; set; }
}

The exception I'm hitting is:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
Amazon.DynamoDBv2.DocumentModel.Table.DDBClient.get returned null.

This seems to be a result of the SDK casting the injected IAmazonDynamoDB instance to AmazonDynamoDBClient when using NETSTANDARD. The Amazon.DynamoDBv2.DocumentModel.Table class's private constructor casts the mocked interface to AmazonDynamoDBClient, but the cast fails and results in null. This is the code which seems to be causing the null.

private Table(IAmazonDynamoDB ddbClient, TableConfig config)
{
	if (config == null)
		throw new ArgumentNullException("config");

	if (ddbClient == null)
		throw new ArgumentNullException("ddbClient");

#if NETSTANDARD
	DDBClient = ddbClient as AmazonDynamoDBClient; // THIS CAUSES NULL
#else
	DDBClient = ddbClient;
#endif
	Config = config;
}

Perhaps I'm using the wrong approach to achieve my goal. If there's any way to do this, I'd appreciate the support. Thanks!

Environment

  • SDK Version: 3
  • Package Version: 3.5.4.29
  • OS Info: Windows 10
  • Build Environment Visual Studio
  • Targeted .NET Platform: .Net Core 3.1

This is a ❓ general question

@kpenergy kpenergy added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Feb 19, 2021
@Kralizek
Copy link
Contributor

This is an old issue that we have also been discussing here: #1589
Nice to see it's not just me feeling the pain.

@ashishdhingra
Copy link
Contributor

Hi @kpenergy,

Good morning.

Thanks for posting guidance question. There are already 2 issues in the backlog:

This is a breaking change and the fix would most likely be incorporated in the next major version of SDK. Could you please add 👍 reaction to both issues so that it gets some traction from developer?

I'm not sure what guidance could be provided here since it is a breaking change. Hope this provides some information.

Thanks,
Ashish

@ashishdhingra ashishdhingra added dynamodb response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Feb 19, 2021
@github-actions
Copy link

github-actions bot commented Mar 6, 2021

This issue has not recieved a response in 2 weeks. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 6, 2021
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness dynamodb guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants