title: How to use Moq to satisfy a MEF import dependency for unit testing?
body: <p>This is my interface</p>
  
  <pre><code>public interface IWork
  {
      string GetIdentifierForItem(Information information);
  }
  </code></pre>
  
  <p>and my class</p>
  
  <pre><code>public class A : IWork
  {
  [ImportMany]
  public IEnumerable&lt;Lazy&lt;IWindowType, IWindowTypeInfo&gt;&gt; WindowTypes { get; set; }
  
  public string GetIdentifierForItem(Information information)
  {
      string identifier = null;
      string name = information.TargetName;
  
      // Iterating through the Windowtypes 
      // searching the 'Name' and then return its ID  
      foreach (var windowType in WindowTypes)
      {
          if (name == windowType.Metadata.Name)
          {
              identifier = windowType.Metadata.UniqueID;
              break;
          }
      }
      return identifier;
  }
  }
  </code></pre>
  
  <p><strong>Problem</strong> : I want to unit test the method <code>GetIdentifierForItem</code> </p>
  
  <p>Here is what I tried doing to solve it - </p>
  
  <p>(1)Create a mock Lazy and set the values that it needs to return on property gets</p>
  
  <p><code>var windowMock = new Mock&lt;Lazy&lt;IWindowType, IWindowTypeInfo&gt;&gt;();
   windowMock.Setup(foo =&gt; foo.Metadata.Name).Returns("Data");
  windowMock.Setup(foo =&gt; foo.Metadata.UniqueID).Returns("someString");</code></p>
  
  <p>(2)Create a window type list and the above mocked object and then set it to the created A object</p>
  
  <pre><code>var WindowTypesList = new List&lt;IWindowType, IWindowTypeInfo&gt;&gt;();
  WindowTypesList.Add(windowMock.Object);
  A a = new A();
  a.WindowTypes = WindowTypesList;
  </code></pre>
  
  <p>(3) Create the information mock</p>
  
  <pre><code>var InfoMock = new Mock&lt;Information&gt;();
  InfoMock.Setup(foo =&gt; foo.TargetName).Returns("Data");
  </code></pre>
  
  <p>To put all of the above together as the unit test</p>
  
  <pre><code>[TestMethod]
  public void GetIDTest()
  {
      var windowMock = new Mock&lt;Lazy&lt;IWindowType, IWindowTypeInfo&gt;&gt;();
      windowMock.Setup(foo =&gt; foo.Metadata.Name).Returns("Data");
      windowMock.Setup(foo =&gt; foo.Metadata.UniqueID).Returns("someString");
  
      var WindowTypesList = new List&lt;Lazy&lt;IWindowType, IWindowTypeInfo&gt;&gt;();
      WindowTypesList.Add(windowMock.Object);
  
      A a = new A();
      a.WindowTypes = WindowTypesList;
      var InfoMock = new Mock&lt;Information&gt;();
      InfoMock.Setup(foo =&gt; foo.TargetName).Returns("Data");
  
      string expected = "someString"; // TODO: Initialize to an appropriate value
      string actual;
      actual = a.GetIdentifierForItem(InfoMock.Object);
      Assert.AreEqual(expected, actual);
  
  }
  </code></pre>
  
  <p>This unit test fails to execute and throws an exception 'TargetInvocationException' and veiwing the detail, it looks like I am doing something that I should not be doing. </p>
  
  <p>But I am not sure how do it other way. I have read some of the links in the Quickstart guide of Moq. I know I am missing something. Can you help me by guiding how to unit test this? </p>