title: Mocking RestClient ExecuteAsync using Nsubstitute
body: <p>Been having a hard time trying to mock out ExecuteAsync method for RestClient (From RestSharp) using Nsubstitute. I have seen an example using Moq (here: <a href="https://www.snip2code.com/Snippet/1389945/Example-for-mocking-RestSharps-IRestClie" rel="nofollow noreferrer">Mocking Restsharp executeasync method using moq</a>) but cannot for the life of me understand why the following code fails using nsubstitute:</p>
  
  <pre><code>RestClientMock.When(x =&gt; x.ExecuteAsync(Arg.Any&lt;IRestRequest&gt;(), Arg.Any&lt;Action&lt;IRestResponse&gt;&gt;()))
                .Do(x =&gt; new RestResponse { StatusCode = HttpStatusCode.NotFound });
  </code></pre>
  
  <p>That's how I set it up and then it would hit the code to be tested like so:</p>
  
  <pre><code> var tcs = new TaskCompletionSource&lt;SendEmailResponse&gt;();
  _restClient.ExecuteAsync(_restRequest, (restResponse) =&gt; {
  
              if (restResponse.StatusCode != HttpStatusCode.OK)
                 tcs.SetResult(new SendEmailResponse(ResponseType.Error, restResponse));
  
              else if (!_enableEmails)
                  tcs.SetResult(new SendEmailResponse(ResponseType.EmailsTurnedOff, restResponse));
  
              else
                  tcs.SetResult(new SendEmailResponse(ResponseType.Sent, restResponse));
  
          });
  
          return tcs.Task;
  </code></pre>
  
  <p>It never seems to execute my callback code in the mock and just hangs as tcs never gets set. Does anybody here have an idea of how to make this work?</p>
  
  <p>EDIT: Resolved. Thanks Nkosi. So I was just newing it up as explained below and not returning. Had I read the documentation more carefully, I'd have seen the section that was mentioned by  David Tchepak in the comments.</p>