How to mock WorkflowHandle for writing unit tests

I have an Api for getting status of Workflow.

public async Task GetStatus(string workflowId)
{
var handle = _temporalClient.GetWorkflowHandle(workflowId);
try
{
var workflowExecutionDescription = await handle.DescribeAsync();
var response = new BaseResponse<(string, List)>
{
Status = “Success”,
Code = 200,
Errors = new Dictionary<string, string>(),
Message = workflowExecutionDescription.Status.ToString()
};
return Ok(response);
}
catch (RpcException ex)
{
var errorResponse = new BaseResponse
{
Status = “Error”,
Code = 400,
Errors = new Dictionary<string, string>(),
Message = $“Workflow with Id {workflowId} does not exist.”
};
return NotFound(errorResponse);
}

How to write unit tests for this? Specifically how can I mock WorkflowHandle and its methods in tests?
I am using Moq and Nunit.

At this time, DescribeAsyncs result of a WorkflowExecutionDescription is not user instantiable. There is an open issue on this. For now, you will have to wrap the client or workflow handle in your own mockable data type if you need to replace the implementation.

1 Like