title: Moq + Unit Testing + System.Reflection.TargetParameterCountException: Parameter Count mismatch
body: <p>I am getting a "System.Reflection.TargetParameterCountException: Parameter Count mismatch" exception when attempting to mock our ApiClient.</p>
  
  <p>I am using the following code to Setup the Moq response</p>
  
  <pre><code>private void SetupApiClientForGetZones(IEnumerable&lt;Zone&gt; zone)
  {
      this.MockHubApiClient.Setup(x =&gt; x.GetAsync&lt;IEnumerable&lt;Zone&gt;&gt;(It.IsAny&lt;string&gt;(), It.IsAny&lt;IDictionary&lt;string, string&gt;&gt;()))
                  .Returns(
                      (string name) =&gt;
                      {
                          return zone == null ? Task.FromResult&lt;IEnumerable&lt;Zone&gt;&gt;(null) : Task.Run(() =&gt; zone);
                      });
  
              this.MockApiClientFactory.Setup(x =&gt; x.CreateClient(It.IsAny&lt;string&gt;()))
                  .Returns(this.MockHubApiClient.Object);
          }
  </code></pre>
  
  <p>The iApiClient interface I attempting to Mock is</p>
  
  <pre><code>public interface IApiClientAsync : IApiClient
      {
          Task&lt;string&gt; GetAsync(string apiController);
  
          Task&lt;T&gt; GetAsync&lt;T&gt;(string apiController) where T : class;
  
          Task&lt;string&gt; GetAsync(string apiController, IDictionary&lt;string, string&gt; param, string queryString);
  
          Task&lt;T&gt; GetAsync&lt;T&gt;(string apiController, IDictionary&lt;string, string&gt; param) where T : class;
  }
  </code></pre>
  
  <p>My unit test is</p>
  
  <pre><code>[Test]
  public void GetZonesNotCached()
  {
      var data = new List&lt;Zone&gt; { new Zone { ZoneId = 1, ZoneName = "Test Zone 1" }, new Zone { ZoneId = 2, ZoneName = "Test Zone 2" } };
  
      this.SetupApiClientForGetZones(data);
  
      this.MockCache.Setup(x =&gt; x.GetItemFromCache&lt;IEnumerable&lt;Zone&gt;&gt;(It.IsAny&lt;string&gt;()));
  
      var organisationService = new OrganisationService(this.MockUnitOfWorkAsync.Object, this.MockApiClientFactory.Object, this.MockCache.Object);
  
      var results = organisationService.GetZones(1, 1).ToList();
  
      Assert.IsNotNull(results);
      Assert.AreEqual(3, results.Count, "There should be 3 records returned");
  
      this.MockCache.Verify(x =&gt; x.GetItemFromCache&lt;IEnumerable&lt;Zone&gt;&gt;(It.IsAny&lt;string&gt;()), Times.Once());
      this.MockHubApiClient.Verify(x =&gt; x.GetAsync&lt;IEnumerable&lt;Zone&gt;&gt;(It.IsAny&lt;string&gt;(), It.IsAny&lt;IDictionary&lt;string, string&gt;&gt;()), Times.Once());
  }
  </code></pre>
  
  <p>I have found numerous other posts with the same exception but none of the solutions or examples are the same as mine.</p>
  
  <p>I have been able to successfully Mock the response when calling the GetAsync method that only has the single string paramter.</p>
  
  <pre><code>    private void SetupApiClientForAllDealerDetails(IEnumerable&lt;DealerDetail&gt; dealerDetails)
      {
          this.MockHubApiClient.Setup(
              x =&gt; x.GetAsync&lt;IEnumerable&lt;DealerDetail&gt;&gt;(It.IsAny&lt;string&gt;()))
              .Returns(
                  (string name) =&gt;
                  {
                      return dealerDetails == null ? Task.FromResult&lt;IEnumerable&lt;DealerDetail&gt;&gt;(null) : Task.Run(() =&gt; dealerDetails);
                  });
  
          this.MockApiClientFactory.Setup(x =&gt; x.CreateClient(It.IsAny&lt;string&gt;()))
              .Returns(this.MockHubApiClient.Object);
      }
  </code></pre>
  
  <p>Any ideas anyone?</p>