title: How to Unit test 2 consecutive calls to the same method with different parameter types
body: <p>Working with C# .NetFrameWork 4.7, Moq 1.4 </p>
  
  <p>I'm testing a method that makes 2 interface calls.  The interface method takes in 1 parameter, and that parameter is an interface as well.  Having a hard time testing this, Moq is throwing casting errors.</p>
  
  <p>2 consecutive calls with the following signiture:</p>
  
  <pre><code>InterfaceUnderTest.MethodUnderTest(IEnumerable&lt;IParamInterface&gt;)
  </code></pre>
  
  <p>The complete code example can be found here on github: <a href="https://github.com/JayZhang727/UnitTestingInterfaceParams" rel="nofollow noreferrer">https://github.com/JayZhang727/UnitTestingInterfaceParams</a></p>
  
  <p>Following is the basic structure:</p>
  
  <p>interfaces and implementations being imported that I can not change:</p>
  
  <pre><code>public interface IWorkInterface
  {
      string DoWork(IEnumerable&lt;IParamInterface&gt; para);
  }
  
  public interface IParamInterface
  {
      IParamDataInterface GetParamData();
  }
  
  public interface IParamDataInterface
  {
      string Id { get; set; }
  }
  
  public class ParamClass&lt;T&gt; : IParamInterface where T : IParamDataInterface
  {
      public T ParamData { get; set; }
  
      public ParamClass()
      {
  
      }
  
      public ParamClass(T para)
      {
          this.ParamData = para;
      }
  
      public IParamDataInterface GetParamData()
      {
          return ParamData;
      }
  }
  
  public class ParamClassA : IParamDataInterface
  {
      public ParamClassA()
      {
          this.Id = "Id";
          this.ParamClassAVar = 123;
      }
  
      public string Id { get; set; }
  
      public int ParamClassAVar { get; set; }
  
  }
  
  public class ParamClassB : IParamDataInterface
  {
      public ParamClassB()
      {
          this.Id = "Id";
          this.ParamClassBVar = "not 123";
      }
  
      public string Id { get; set; }
  
      public string ParamClassBVar { get; set; }
  
  }
  </code></pre>
  
  <p>Here is the class under test:   </p>
  
  <pre><code>public ClassUnderTest(IWorkInterface workInt)
  {
      this.WorkClient = workInt;
  }
  
  public IWorkInterface WorkClient { get; set; }
  
  public string MethodUnderTest()
  {
      var result = string.Empty;
  
      //Class A is an implementation of IParamDataInterface
      var a = new ParamClassA();
      var theParamA = new ParamClass&lt;ParamClassA&gt;(a);
      var listA = new List&lt;IParamInterface&gt;();
      listA.Add(theParamA);
  
      result = WorkClient.DoWork(listA);
  
      //Class B is also an implementation of IParamDataInterface
      var b = new ParamClassB();
      var theParamB = new ParamClass&lt;ParamClassB&gt;(b);
      var listB = new List&lt;IParamInterface&gt;();
      listB.Add(theParamB);
  
      result = result + WorkClient.DoWork(listB);
  
      return result;
  }
  </code></pre>
  
  <p>Here is the test calls that I tried:</p>
  
  <pre><code>private Mock&lt;IWorkInterface&gt; mockClient;
  
  [TestMethod()]
  public void DoWorkTest()
  {
      mockClient = new Mock&lt;IWorkInterface&gt;(MockBehavior.Strict);
      var target = new ClassUnderTest(mockClient.Object);
  
      var mockSquence = new MockSequence();
  
      mockClient.InSequence(mockSquence).Setup(ec =&gt; ec.DoWork(It.Is&lt;List&lt;IParamInterface&gt;&gt;(el =&gt; ((ParamClassA)((ParamClass&lt;ParamClassA&gt;)el[0]).GetParamData()).ParamClassAVar == 123))).Returns("123");
  
      mockClient.InSequence(mockSquence).Setup(ec =&gt; ec.DoWork(It.Is&lt;List&lt;IParamInterface&gt;&gt;(el =&gt; ((ParamClassB)((ParamClass&lt;ParamClassB&gt;)el[0]).GetParamData()).ParamClassBVar == "not 123"))).Returns("not 123");
  
      //act
      target.MethodUnderTest();
  
  
      //assert
      mockClient.VerifyAll();
  }
  </code></pre>
  
  <p>I get errors about can not cast from Class A to Class B:</p>
  
  <pre><code>Message: Test method ClassUnderTesting.UnitTests.ClassUnderTestingTests.DoWorkTest threw exception: 
  System.InvalidCastException: Unable to cast object of type 'ClassUnderTesting.ParamClass`1[ClassUnderTesting.ParamClassA]' to type 'ClassUnderTesting.ParamClass`1[ClassUnderTesting.ParamClassB]'.
  </code></pre>
  
  <p>It seems that Moq is not doing it in sequence and for some reason, the second setup is trumping the first setup and sequence doesn't seem to help. Anyone know how I should setup and test these 2 calls?</p>