title: How to test async methods with Moq and xUnit
body: <p>I am having the following method which calls an insert and update methods.</p>
  <p>Conditions</p>
  <ol>
  <li><p>In the below <code>ProcessBspLoan</code> function, I am calling other function named <code>getBspData</code>
  if <code>getBspData</code> returns any result. I will <strong>insert</strong> the result
  and <strong>update</strong> the database as a success for a particular Id</p>
  </li>
  <li><p>if <code>getBspData</code> throws any Exception I will only <strong>update</strong> the database as a fail for a particular Id without <strong>inserting</strong></p>
  </li>
  </ol>
  <p>Here is the ProcessBspLoan function in the given class</p>
  <pre><code>    public class BspLoanProcessor : IBspLoanProcessor
      {
          private readonly IBspClient _bspService;
          private readonly IBspRepository _bspRepository;
          private readonly ILogger&lt;BspLoanProcessor&gt; _logger;
          private readonly IMapper _mapper;
          private readonly IFhaRepository _fhaRepository;
  
          public BspLoanProcessor(IBspClient bspService, ILogger&lt;BspLoanProcessor&gt; logger, 
              IMapper mapper, IBspRepository bSPRepository, IFhaRepository fhaRepository)
          {
              _bspService = bspService;
              _logger = logger;
              _mapper = mapper;
              _bspRepository = bSPRepository;
              _fhaRepository = fhaRepository;
          }
  
          public async Task&lt;bool&gt; ProcessBspLoan(BspLoanDetails bspLoanDetails, string transactionId, string triggerType)
          {
              FhaTransactionDetails fhaData = _mapper.Map&lt;FhaTransactionDetails&gt;(bspLoanDetails);
  
              try
              {
                  ////////////////////////////Calling getBspData///////////////////////////////////////
                  var result = await _bspService.getBspData(bspLoanDetails.NsmLoanNumber, transactionId);
  
                  result.TransactionId = transactionId;
  
                  await _bspRepository.InsertBspResponse(result);
                  
                  await _fhaRepository.UpdateFhaTransactionWithLoanNumber(transactionId,&quot;SUCCESS&quot;);
                  return true;
              }
              catch(Exception ex)
              {
                  await _fhaRepository.UpdateFhaTransactionWithLoanNumber(transactionId,&quot;FAIL&quot;);
                  return false;
              }
          }
      }
  
  </code></pre>
  <p>I wrote the test method for the above function want to check it is returning true or false based on inputs provided</p>
  <p>Here is my test function</p>
  <pre><code>    public async Task Test1Async()
      {
          Mock&lt;IBspLoanProcessor&gt; _bspLoanProcessor = new Mock&lt;IBspLoanProcessor&gt;();
          Mock&lt;IBspRepository&gt; _bspRepository = new Mock&lt;IBspRepository&gt;();
          Mock&lt;IFhaRepository&gt; _fhaRepository = new Mock&lt;IFhaRepository&gt;();
          Mock&lt;IBspClient&gt; _bspClient = new Mock&lt;IBspClient&gt;();
          BspLoanDetails bspLoanDetails = new BspLoanDetails
          {
              TriggerType = &quot;BLOB&quot;,
              Attempts = 1,
              FirstRunDateTime = DateTime.Now.ToUniversalTime()
          };
          ----&gt; 1
          _bspClient.Setup(x =&gt; x.getBspData(It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;())).Returns(Task.FromResult(new BspResponseDetails()));
          ----&gt; 2
          _bspRepository.Setup(x =&gt; x.InsertBspResponse(It.IsAny&lt;BspResponseDetails&gt;())).Returns(Task.CompletedTask);
          ----&gt; 3
          _fhaRepository.Setup(x =&gt; x.UpdateFhaTransactionWithLoanNumber(It.IsAny&lt;string&gt;(),It.IsAny&lt;string&gt;())).Returns(Task.CompletedTask);
          bool value = await _bspLoanProcessor.Object.ProcessBspLoan(bspLoanDetails, &quot;123&quot;, &quot;SCHEDULE&quot;);
          Assert.True(value);
      }
  </code></pre>
  <p>In the above the test, I am checking the first condition</p>
  <ol>
  <li>I am returning data as an object whenever anyone called gets data</li>
  <li>Inserting <code>BspResponsemethod</code> also I am returning <code>task.completedTask</code></li>
  <li><code>UpdateFhaTransactionWithLoanNumber</code> also I am returning <code>task.completedTask</code></li>
  </ol>
  <p>The actual expected output is true, but it is returning false.</p>
  <p>I am new to Moq and xUnit. Please help me to resolve this issue and also test async and await methods.</p>
  <p>IBspClient interface</p>
  <pre><code>    public interface IBspClient
      {
          Task&lt;BspResponseDetails&gt; getBspData(string loanNumber,string tid);
  
          Task&lt;BspHealthCheck&gt; GetHealthStatus();
      }
  </code></pre>
  <p>IBspRepository Interface</p>
  <pre><code>    public interface IBspRepository
      {
          Task InsertBspResponse(BspResponseDetails bsp);
      }
  </code></pre>
  <p>IFhaRepository interface</p>
  <pre><code>    public interface IFhaRepository
      {
          Task UpdateFhaTransactionWithLoanNumber(string tid, string status);    
      }
  </code></pre>
  <p>Thanks in advance</p>