title: C# Unit Test method with MEF Imports
body: <p>
  Hi!</p>
  
  <p>I have a class which looks like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>public class ItemCollector {
    public IKnownAuthority _DefaultAuthority = new DefaultAuthority();
  
    [ImportMany(typeof(IKnownAuthority))]
    private IEnumerable&lt;Lazy&lt;IKnownAuthority, IKnownAuthorityMetadata&gt;&gt; _KnownAuthorities { get; set; }
  
    public ItemCollector() {
      var catalog = new AggregateCatalog();
      catalog.Catalogs.Add(new DirectoryCatalog(
                  Path.Combine(Environment.CurrentDirectory, AUTHORITY_FOLDER)));
  
      var container = new CompositionContainer(catalog);
      container.ComposeParts(this);
    }
  
    public IEnumerable&lt;Items&gt; GetItems(string Auth) {
      var Authority = _KnownAuthorities.FirstOrDefault(x =&gt; 
                              x.Metadata.AuthorityName.ToLowerInvariant() == 
                              Auth.ToLowerInvariant());
  
      if (Authority == null)
        return _DefaultAuthority.GetItems(Address);
  
      return Authority.Value.GetItems(Address);
    }
  }
  </code></pre>
  
  <p>As you can see there is a method (<strong>GetItems</strong>) and I want to Unit Test this method. The method just tries to find a value in the MEF container and return eather the result of the found item or a default value. (So I dont want to test the MEF stuff [I think someone else have allready done this :)] I just want to test if the method correctly returns the value of the Default Item or the Imported Item regarding the parameter)</p>
  
  <p>And here is my problem. I can create a test to check the default value but I am not able to create a test with a stub for a specific "Authority" assuming the Directory (For the DirectoryCatalog) is empty. So is there a way to "inject" a Stub[IKnownAuthority] to the allready composed catalog from the test method?</p>
  
  <p>I have tried something like this:</p>
  
  <pre class="lang-c# prettyprint-override"><code>[TestMethod]
  public void GetItems_Ok()
  {
    ItemsCollector collector = new ItemsCollector();
    collector._DefaultAuthority = new StubDefaultAuthority_01();
  
    CompositionContainer container = new CompositionContainer();
    container.ComposeExportedValue&lt;IKnownAuthority&gt;(new StubAuthority_02());
    container.ComposeParts(collector);
  
    var list = collector.GetItems("testing.test"));      
    Assert.IsTrue(list.Count() &gt; 0);
  }
  
  [Export(typeof(IKnownAuthority))]
  [ExportMetadata("AuthorityName", "testing.test")]
  public class StubAuthority_02 : IKnownAuthority
  {
    public IEnumerable&lt;Items&gt; GetItems(string Auth) {
      return new List&lt;Items&gt;() { new Items() };
    }
  }
  </code></pre>
  
  <p>The StubAuthority_02 won't find it's way to the _KnownAuthorities collection. First I thought the reason is the fact that I compose the parts in the constructor and then again (Recomposition without the Attributes set) in the test method. But if I remove the </p>
  
  <pre class="lang-c# prettyprint-override"><code>container.ComposeParts(this);
  </code></pre>
  
  <p>line from the ItemCollector constructor the StubAuthority_02 isn´t there anyway. I am sure this is just a problem in understanding but I couldn´t figure it out...</p>