I have a Durable Azure Function I just moved over to .NET 8 isolated worker model, from .NET 6 in-proc.
I am struggling with unit testing/mocking return data of type EntityMetadata
for DurableEntityClient.GetEntityAsync<T>()
.
Given the following code i want to test/mock, which is used in my Durable Function:
async Task<MyState?> GetEntityValueAsync(DurableTaskClient client, EntityInstanceId entityId)
{
// this is the new way to get entity state in .NET 8 isolated
var entity = await client.Entities.GetEntityAsync<MyType>(entityId);
return await entity.State.Get();
}
I would expect to be able to write a unit test like this, mocking the services and setting up the expected return data:
[Theory]
[AutoMockData]
public async Task MyTest(
[Frozen] DurableTaskClient taskClient,
MyFunction function,
MyMessage message,
MyState state)
{
// arrange
var entityId = new EntityInstanceId(nameof(MyType), message.ContextId.ToString());
var entityState = new EntityMetadata<MyType>(entityId);
var durableClientMock = new Mock<DurableTaskClient>("test");
var entityClient = new Mock<DurableEntityClient>("test");
durableClientMock
.Setup(d => d.Entities).Returns(entityClient.Object);
entityClient
.Setup(d => d.GetEntityAsync<MyType>(It.IsAny<EntityInstanceId>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(entityState);
// Act
// GetEntityValueAsync is called in .DoTheThing()
await function.DoTheThing(JsonConvert.SerializeObject(message), taskClient);
// Assert
// stuff should work.
}
However, this does not work. When the test runs, and it tries to call:
var entity = await client.Entities.GetEntityAsync<MyType>(entityId);
I get the error:
System.ArgumentException : Can not instantiate proxy of class: Microsoft.DurableTask.Client.Entities.EntityMetadata`1.
Could not find a parameterless constructor. (Parameter 'constructorArguments')
How can I fix this? How should I properly be mocking EntityMetadata
responses? It is a sealed class so I cannot extend from it and make my own test implementation.