title: Polly verify how many times httpclient timed out/retried in a Unit Test
body: <p>This is not a fully working solution, just parts of the code to make the question cleaner and more readable.</p>
  
  <p>I have read the Polly's documentation <a href="https://github.com/App-vNext/Polly/wiki/Unit-testing-with-Polly#2-i-want-to-unit-test-that-the-polly-policies-i-apply-actually-do-what-they-say-on-the-tin" rel="nofollow noreferrer">here</a>, however I really need to test whether the following TimeoutPolicy's <code>onTimeoutAsync</code> delegate is being hit (also, in case of wrapping TimeoutPolicy along with RetryPolicy - how many <strong>TIMES</strong> it has been hit) after the httpClient times out:</p>
  
  <pre><code>public static TimeoutPolicy&lt;HttpResponseMessage&gt; TimeoutPolicy
  {
      get
      {
          return Policy.TimeoutAsync&lt;HttpResponseMessage&gt;(1, onTimeoutAsync: (context, timeSpan, task) =&gt;
          {
              Console.WriteLine("Timeout delegate fired after " + timeSpan.TotalMilliseconds);
              return Task.CompletedTask;
          });
      }
  }
  </code></pre>
  
  <p>TimeoutPolicy is set to timeout after 1 second of not receiving anything from HTTP client (HttpClient's mock is delayed for 4 seconds as shown below)</p>
  
  <pre><code>var httpMessageHandler = new Mock&lt;HttpMessageHandler&gt;();
  
  httpMessageHandler.Protected()
  .Setup&lt;Task&lt;HttpResponseMessage&gt;&gt;("SendAsync", ItExpr.IsAny&lt;HttpRequestMessage&gt;(), ItExpr.IsAny&lt;CancellationToken&gt;())
  .Callback(() =&gt; Thread.Sleep(4000))//Delayed here
  .Returns(() =&gt; Task.FromResult(new HttpResponseMessage
  {
      StatusCode = HttpStatusCode.InternalServerError,
      Content = new StringContent("Some response body", Encoding.UTF8, "text/xml")
  }));
  
  var httpClient = new HttpClient(httpMessageHandler.Object);
  </code></pre>
  
  <p>Policies are mocked to be able to call .Verify() on it for Assertion:</p>
  
  <pre><code>var mockedPolicies = new Mock&lt;Policies&gt;();
  </code></pre>
  
  <p>I execute the call with a mocked HTTP Client and a mocked Timeout policy:</p>
  
  <pre><code>await mockedPolicies.Object.TimeoutPolicy.ExecuteAsync(ct =&gt; _httpClient.PostAsync("", new StringContent("Some request body"), ct), CancellationToken.None);
  </code></pre>
  
  <p>Assertion:</p>
  
  <pre><code>mockedPolicies.Verify(p =&gt; p.TimeoutDelegate(It.IsAny&lt;Context&gt;(), It.IsAny&lt;TimeSpan&gt;(), It.IsAny&lt;Task&gt;()), Times.Exactly(1));
  </code></pre>
  
  <p>However, test outcome says it has been called 0 times instead of 1.
  Thank you for any answers.</p>