title: NSubstitute Async returns null despite defined return object
body: <p>I have a unit test that should return the specified object but it is returning null.</p>
  
  <p>The Data Provider to test:</p>
  
  <pre><code>public class PlanDataProvider : BaseDomainServiceProvider, IPlanDataProvider
  {
      //CTOR
      public PlanDataProvider(IDataAccessTemplate template, IEntityStore entityStore) : base(template, entityStore)
      {
      }
  
      public async Task&lt;DefaultActionPlan&gt; GetDefaultActionPlan(string referenceListId)
      {
          var objectId = GetObjectId(referenceListId);
  
          var defaultActionPlan = await Template.InvokeAsync(context =&gt; Task.FromResult(EntityStore.GetEntityById&lt;DefaultActionPlan, ObjectId&gt;
              (
                  context.ActivityContext as IDataAccessContext,
                  typeof(DefaultActionPlan).FullName,
                  objectId
              )));
      }
  }
  </code></pre>
  
  <p>The Test:</p>
  
  <pre><code>public async Task GetPlan_BadPlanID()
  {
     //Arrange
     string badPlanId = "57509afbc6b48d3f33b2dfcd";
  
     ...snip...
  
     DefaultActionPlan jj = new ObjectId(badPlanId);
  
     //create EntityStore object
     var dataxs = Substitute.For&lt;IDataAccessContext&gt;();
     var estore = Substitute.For&lt;IEntityStore&gt;();
     estore.GetEntityById&lt;DefaultActionPlan, ObjectId&gt;(
          dataxs, 
          typeof(DefaultActionPlan).FullName, 
          new ObjectId(badPlanId))
     .Returns(Task.FromResult(jj).Result);
  
     var dataAccessTemplate = Substitute.For&lt;IDataAccessTemplate&gt;();
  
     PlanDataProvider pdp = new PlanDataProvider(dataAccessTemplate, estore);
  
     //Act
     var t = await pdp.GetDefaultActionPlan(badPlanId);
     //Now this confuses me as the compiler thinks t is DefaultActionPlan NOT Task&lt;DefaultActionPlan&gt;???
  }
  </code></pre>
  
  <p>Anyway t returns null eferytime and debugging the test t is null because the GetDefaultActionPlan is not returning jj but instead null??</p>
  
  <p>What am I missing to have jj returned?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>Both Eris and Gabe rightly pointed out that my Mock of the IEntityStore was not sufficient as that value...even though it specifies a return will not get passed to the wrapping InvokeAsync thus I needed to Mock the InvokeAsync as well.</p>
  
  <p>Gabe's answer was slightly off as passing Arg.Any does not satisfy the InvokeAsync needed parms. However I do not fault him for this as I spent an hours tracking the inheritance chain across multiple projects (this is a big company). Something he does not have access to do.</p>
  
  <p>In the end here is the code that resulted in success:</p>
  
  <pre><code>var estore = Substitute.For&lt;IEntityStore&gt;();
  var dataAccessTemplate = Substitute.For&lt;IDataAccessTemplate&gt;();
  dataAccessTemplate.InvokeAsync(context =&gt; Task.FromResult(
        estore.GetEntityById&lt;DefaultActionPlan&gt;(
                 dataxs, typeof(DefaultActionPlan).FullName, new ObjectId(badPlanId))))
  .ReturnsForAnyArgs(jj);
  
  var pdp = new PlanDataProvider(dataAccessTemplate, estore);
  </code></pre>