title: How to mock an IEnumerable<interface> type and pass it to constructor
body: <p>I got a class which looks like below</p>
  
  <pre><code>public interface ILocationProvider
  {
      bool IsRequiredLocation (string type);
  }
  
  public class MyClass : IMyInterface
  {
      private readonly IEnumerable&lt;ILocationProvider&gt; _locationProvider;
      public MyClass (ILocationProvider[] locationProvider)
      {
          _locationProvider = locationProvider;
      }
      public ILocationProvider ProvideRequireLocationObject(string type)
      {
          ILocationProvider location = _locationProvider.FirstOrDefault(x =&gt; x.IsRequiredLocation(type));
          return location;
      }
  }
  </code></pre>
  
  <p>Now I am trying to write some tests for it. But I stuck passing the <code>Mock&lt;IEnumerable&lt;ITransitReportCountryFlowProvider&gt;&gt;</code> to constructor. Below my test code</p>
  
  <pre><code>[TestClass]
  public class TMyClassTest
  {
      private Mock&lt;IEnumerable&lt;ILocationProvider&gt;&gt; _locationProvider = null;
      private IMyInterface _myClass = null;
      [TestInitialize]
      public void InitializeTest ()
      {
          _locationProvider = new Mock&lt;IEnumerable&lt;ILocationProvider&gt;&gt;();
      }
      [TestMethod]
      public void ProvideRequireLocationObject_Test1()
      {
          //Given: I have type as 'PMI'
          string type = "PMI";
          //When: I call MyClass object
          _myClass = new MyClass(_locationProvider.Object); //wrong actual argument as the formal argument is an array of ILocationProvider
          //_locationProvider.Setup(x =&gt; x.IsRequiredCountryFlow(It.IsAny&lt;string&gt;())).Returns(true); //how do I setup
          ILocationProvider result = _myClass.ProvideRequireLocationObject(type);
          //Then: I get a type of ILocationProvider in return
          Assert.IsTrue(result is ILocationProvider);
      }
  }
  </code></pre>
  
  <p><strong>Problem 1:</strong> The line <code>_myClass = new MyClass(_locationProvider.Object)</code> in above test class, as the constructor's formal argument is <code>ILocationProvider[]</code> so I cannot pass a mocking object of <code>Mock&lt;IEnumerable&lt;ILocationProvider&gt;&gt;</code></p>
  
  <p><strong>Problem 2:</strong> If I change the line <code>private readonly IEnumerable&lt;ILocationProvider&gt; _locationProvider;</code> in above <code>MyClass</code> to <code>private readonly ILocationProvider[] _locationProvider;</code> I will not be able to mock it as because mock must be an interface or an abstract or non-sealed class.</p>
  
  <p><strong>Problem 3:</strong> How do I set up for <code>_locationProvider.FirstOrDefault(x =&gt; x.IsRequiredLocation(type));</code> in my test method</p>
  
  <p><strong>Problem 4:</strong> How do I assert that my method <code>ProvideRequireLocationObject</code> is returning a type of <code>ILocationProvider</code></p>