title: How to mock a method to throw a particular exception in C#
body: <p>I'm trying to mock a method to throw a particular exception, but not able to achieve that. My method looks like -</p>
  <pre><code>    try
      {
          if (!ledgerStoreRequest.LedgerSummary.IsNullOrEmpty())
          {
              await _ledgerStoreHelper.WriteLedgerSummary(ledgerStoreRequest, ref storedSummaryCount, businessUnitId);
          }
      }
      catch (ArgumentException exception)
      {
          var errorMessage = $&quot;Error: DynamoDB insertion has failed with duplicate key. {exception.Message}&quot;;
          _logger.LogError($&quot;{errorMessage}&quot;);
          throw new Ascendon.SDK.Contract.Fault.InvalidPropertyException(new Fault
          {
              Code = AccountErrorCode.InsertionToLedgerSummaryTblFailed,
              Message = $&quot;LedgerStore API failure. DynamoDB insertion has failed for &quot; +
                        $&quot;BusinessUnit: {businessUnitId} SubscriberID: {ledgerStoreRequest.SubscriberId}.&quot;
          });
      }
  </code></pre>
  <p>So I'm trying to mock <strong>WriteLedgerSummary</strong> to return <code>InvalidPropertyException</code> exception in an unit test. Currently the way I'm doing it is -</p>
  <pre><code>    // Arrange
      LedgerStoreHelper
          .Setup(m =&gt;
              m.WriteLedgerSummary(It.Ref&lt;LedgerStoreRequest&gt;.IsAny, ref It.Ref&lt;int&gt;.IsAny, It.Ref&lt;int&gt;.IsAny))
          .Throws(new InvalidPropertyException(new Fault
          {
              Code =  GlobalErrorCode.InvalidProperty,
              Message = &quot;some error&quot;
          }));
  
      // Act
      var response = Assert.ThrowsAsync&lt;InvalidPropertyException&gt;(() =&gt; 
          _repository.StoreLedgerEntityAsync(BusinessUnitId,
              LedgerStoreTestConstants.LedgerStoreRequestInvoiceSummaryItem));
  </code></pre>
  <p>But not able to see the exception in response. Am I missing something here?</p>