title: In C#, what is wrong with my negative test of TryAsync?
body: <p>I have the following method:</p>
  <pre><code>public TryAsync&lt;bool&gt; TryRelay(
      MontageUploadConfig montageData,
      File sourceFile,
      CancellationToken cancellationToken
  ) =&gt; new(async () =&gt;
  {
      byte[] fileContent = await _httpClient.GetByteArrayAsync(sourceFile.Url, cancellationToken);
      return await _attachmentController.TryUploadAttachment(montageData.EventId, fileContent, sourceFile.Name);
  });
  </code></pre>
  <p>I created a couple tests to prove it works as expected.
  My test for a negative case is failing.</p>
  <p>This is the test:</p>
  <pre><code>[Fact]
  public static async Task TestRelayShouldCatchErrorsGettingFile()
  {
      // Arrange
      Mock&lt;IAttachmentControllerV6&gt; mockAttachmentController = new();
      Mock&lt;HttpMessageHandler&gt; mockHttpMessageHandler = new();
      MontageUploadTaskProcessor mockProcessorUnderTest = CreateProcessor(mockAttachmentController, mockHttpMessageHandler);
  
      MontageUploadConfig montageData = new()
      {
          EventId = &quot;Test001&quot;
      };
      File sourceFile = new()
      {
          Name = &quot;Test.pdf&quot;,
          Url = &quot;https://www.example.com/test.pdf&quot;
      };
      CancellationToken cancellationToken = default;
  
      const string message = &quot;Expected Exception&quot;;
      mockHttpMessageHandler.SetupAnyRequest()
          .Throws(new SalesforceCacheException(message));
  
      // Act
      Result&lt;bool&gt; result = await mockProcessorUnderTest.TryRelay(montageData, sourceFile, cancellationToken)();
  
      // Assert
      Assert.True(result.IsFaulted);
      result.IfFail(exception =&gt;
      {
          Assert.True(exception is Exception);
          Assert.Equal(message, exception.Message);
      });
  }
  </code></pre>
  <p>This is the error:</p>
  <blockquote>
  <p>WorkflowTests.TaskProcessor.OldOrg.MontageUploadTaskProcessorUnitTests.TestRelayShouldCatchErrorsGettingFile
  Source: MontageUploadTaskProcessorUnitTests.cs line 59    Duration:
  144 ms</p>
  <p>Message:  SFCacheController.SalesforceCacheException : Expected
  Exception</p>
  <p>Stack Trace:  ThrowException.Execute(Invocation invocation) line 22
  MethodCall.ExecuteCore(Invocation invocation) line 97
  Setup.Execute(Invocation invocation) line 85
  FindAndExecuteMatchingSetup.Handle(Invocation invocation, Mock mock)
  line 107 IInterceptor.Intercept(Invocation invocation) line 17
  Interceptor.Intercept(IInvocation underlying) line 107
  AbstractInvocation.Proceed()
  HttpMessageHandlerProxy.SendAsync(HttpRequestMessage request,
  CancellationToken cancellationToken)
  HttpMessageInvoker.SendAsync(HttpRequestMessage request,
  CancellationToken cancellationToken)
  HttpClient.SendAsyncCore(HttpRequestMessage request,
  HttpCompletionOption completionOption, Boolean async, Boolean
  emitTelemetryStartStop, CancellationToken cancellationToken)
  HttpClient.GetByteArrayAsyncCore(HttpRequestMessage request,
  CancellationToken cancellationToken) &lt;b__0&gt;d.MoveNext() line
  140
  --- End of stack trace from previous location --- MontageUploadTaskProcessorUnitTests.TestRelayShouldCatchErrorsGettingFile()
  line 81
  --- End of stack trace from previous location ---</p>
  </blockquote>
  <p>The exception seems to be thrown after <code>TryRelay()</code> is invoked, but even before any assertions are attempted.</p>
  <p>Am I wrong to expect <code>TryAsync</code> will catch and box the exception?
  Am I wrong to expect this to work in a test context?
  What do I need to do to make this test pass?</p>