title: C# Multiple Stub implementation for method with T
body: <p>Currently I have a factory class where I have method to instantiate different classes using reflection and return a base interface which all classes implement. The code works fine in general. But when I turned to write unit test (ms-test) on the class I'm running into issues when I try to instantiate multiple classes</p>
  
  <p>Factory class:</p>
  
  <pre><code>public class BusinessFactory : IBusinessFactory
  {
          public BusinessFactory()
          {
          }
  
          public IBaseBusiness Create&lt;T&gt;() where T : class
          {
              return (IBaseBusiness) Activator.CreateInstance(typeof(T));
          }
  }
  </code></pre>
  
  <p>Unit test:</p>
  
  <p>I tried to stub the <code>IBusinessFactory</code> and inject into my class under test so I can fake the implementations that the class uses.</p>
  
  <p>Example: failing to complete TestInit method, and it's failing when instantiating stub of class2.</p>
  
  <pre><code>interface IClass1 : IBaseBusiness{}
  
  public Class1 : IClass1{}
  
  interface IClass2 : IBaseBusiness{}
  
  public Class2 : IClass2{}
  
  public TestFactory{
  
  [TestInitialize]
  public void TestInit()
  {
      var fkBusFac = new StubIBusinessFactory();
  
      fkBusFac.CreateOf1&lt;Class1&gt;(() =&gt; new StubIClass1() { });
      fkBusFac.CreateOf1&lt;Class2&gt;(() =&gt; new StubIClass2() { });
  }
  </code></pre>
  
  <p>Can anyone please help?</p>