title: How to unit test Durable Functions with errors and retry
body: <p>I've read these two Microsoft documentation on Unit Testing and Error Handling</p>
  
  <p>Unit testing:
  <a href="https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing</a></p>
  
  <p>Error handling - with the retry
  <a href="https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-error-handling?tabs=csharp" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-error-handling?tabs=csharp</a></p>
  
  <p>What I am trying to do in my unit test the following:</p>
  
  <p>1) have activity throw exception</p>
  
  <p>2) verify Orchestrator will call <code>CallActivityRetryAsync</code> based on my retry options, e.g. 3 times</p>
  
  <p>3) verify my try/catch block catches the exception once and my error handler is called. (aside: is the exception wrapped in a <code>FunctionFailedException</code>?) </p>
  
  <p>So for example, based on the MS documentation, if this is my orchestrator:</p>
  
  <pre><code>    [FunctionName("E1_HelloSequence")]
      public static async Task&lt;List&lt;string&gt;&gt; Run(
          [OrchestrationTrigger] IDurableOrchestrationContext context)
      {
      try
      {
          var outputs = new List&lt;string&gt;();
          var retryOptions = new RetryOptions(
                  firstRetryInterval: TimeSpan.FromSeconds(5),
                  maxNumberOfAttempts: 3);
  
  
          outputs.Add(await context.CallActivityRetryAsync&lt;string&gt;("E1_SayHello", retryOptions, "Tokyo"));
          outputs.Add(await context.CallActivityRetryAsync&lt;string&gt;("E1_SayHello", retryOptions, "Seattle"));
  
  
          // returns ["Hello Tokyo!", "Hello Seattle!"]
          return outputs;
      }
      catch (Exception ex)
      {
              await context.CallActivityAsync&lt;string&gt;("E1_SayHello", "I Failed..."));
              throw;
      }
      }
  
      [FunctionName("E1_SayHello")]
      public static string SayHello([ActivityTrigger] IDurableActivityContext context)
      {
          string name = context.GetInput&lt;string&gt;();
          return $"Hello {name}!";
      }
  </code></pre>
  
  <p>And my test would be something like this:</p>
  
  <pre><code>public class HelloSequenceTests
  {
      [Fact]
      public async Task Run_returns_multiple_greetings()
      {
          var mockContext = new Mock&lt;IDurableOrchestrationContext&gt;();
          mockContext.Setup(x =&gt; x.CallActivityRetryAsync&lt;string&gt;("E1_SayHello", "Tokyo")).ReturnsAsync("Hello Tokyo!");
          mockContext.Setup(x =&gt; x.CallActivityRetryAsync&lt;string&gt;("E1_SayHello", "Seattle")).ReturnsAsync("Hello Seattle!");
  
          var result = await HelloSequence.Run(mockContext.Object);
  
          Assert.Equal(2, result.Count);
          Assert.Equal("Hello Tokyo!", result[0]);
          Assert.Equal("Hello Seattle!", result[1]);
      }
  }
  </code></pre>
  
  <p>So in the example above, how would I</p>
  
  <p>1) have the Activity <code>E1_SayHello</code> thrown an exception</p>
  
  <p>2) verify that there were 3 retries</p>
  
  <p>3) verify that my catch block executed once?</p>