How to mock a MEF ExportFactory using Moq instances?











up vote
2
down vote

favorite












How can I mock a Managed Extensibility Framework (MEF) import into an ExportFactory[IMyType[T]], using mocks of IMyType[T] (being itself a generic interface)? Or, in general, specify instances to be returned by the ExportFactory, which can't be created with a constructor alone?



Moq can't create a mock instance directly, as far as I know. I want an alternative to implementing the whole interface, which may be much larger and change, while I don't want to change the test.



I once found a very complex ExportProvider code, with key-value strings, which didn't work if the generic type of the ExportFactory[T] was itself generic.



// actual generic type is meaningless here, but it is a generic interface
public interface IMyType<T>
{
string GetMessage();
}

public class FactoryImporter<T>
{
[Import]
ExportFactory<IMyType<T>> MyTypeFactory {get;set;}
}

public class Tester
{
public void TestFactoryImporter()
{
Func<IMyType<string>> createMockFunc = () =>
{
var mock = new Mock<IMyType<string>>();
mock.Setup(m => m.GetMessage()).Returns("I'm a mocked IMyType<string>");
return mock.Object;
}

var regBuilder = new RegistrationBuilder();

// pseudo-code, how to do this in reality?
regBuilder.ForType<IMyType<string>>().CreateInstance(createMockFunc);

var catalog = new AggregateCatalog();
var appCatalog = new ApplicationCatalog(regBuilder);
catalog.Catalogs.Add(appCatalog);
var container = new CompositionContainer(catalog);

var factoryImporter = new FactoryImporter<string>();
container.ComposeParts(factoryImporter);

Assert.AreEqual(
"I'm a mocked IMyType<string>",
factoryImporter.MyTypeFactory.GetExport().Value.GetMessage());
}
}









share|improve this question




























    up vote
    2
    down vote

    favorite












    How can I mock a Managed Extensibility Framework (MEF) import into an ExportFactory[IMyType[T]], using mocks of IMyType[T] (being itself a generic interface)? Or, in general, specify instances to be returned by the ExportFactory, which can't be created with a constructor alone?



    Moq can't create a mock instance directly, as far as I know. I want an alternative to implementing the whole interface, which may be much larger and change, while I don't want to change the test.



    I once found a very complex ExportProvider code, with key-value strings, which didn't work if the generic type of the ExportFactory[T] was itself generic.



    // actual generic type is meaningless here, but it is a generic interface
    public interface IMyType<T>
    {
    string GetMessage();
    }

    public class FactoryImporter<T>
    {
    [Import]
    ExportFactory<IMyType<T>> MyTypeFactory {get;set;}
    }

    public class Tester
    {
    public void TestFactoryImporter()
    {
    Func<IMyType<string>> createMockFunc = () =>
    {
    var mock = new Mock<IMyType<string>>();
    mock.Setup(m => m.GetMessage()).Returns("I'm a mocked IMyType<string>");
    return mock.Object;
    }

    var regBuilder = new RegistrationBuilder();

    // pseudo-code, how to do this in reality?
    regBuilder.ForType<IMyType<string>>().CreateInstance(createMockFunc);

    var catalog = new AggregateCatalog();
    var appCatalog = new ApplicationCatalog(regBuilder);
    catalog.Catalogs.Add(appCatalog);
    var container = new CompositionContainer(catalog);

    var factoryImporter = new FactoryImporter<string>();
    container.ComposeParts(factoryImporter);

    Assert.AreEqual(
    "I'm a mocked IMyType<string>",
    factoryImporter.MyTypeFactory.GetExport().Value.GetMessage());
    }
    }









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      How can I mock a Managed Extensibility Framework (MEF) import into an ExportFactory[IMyType[T]], using mocks of IMyType[T] (being itself a generic interface)? Or, in general, specify instances to be returned by the ExportFactory, which can't be created with a constructor alone?



      Moq can't create a mock instance directly, as far as I know. I want an alternative to implementing the whole interface, which may be much larger and change, while I don't want to change the test.



      I once found a very complex ExportProvider code, with key-value strings, which didn't work if the generic type of the ExportFactory[T] was itself generic.



      // actual generic type is meaningless here, but it is a generic interface
      public interface IMyType<T>
      {
      string GetMessage();
      }

      public class FactoryImporter<T>
      {
      [Import]
      ExportFactory<IMyType<T>> MyTypeFactory {get;set;}
      }

      public class Tester
      {
      public void TestFactoryImporter()
      {
      Func<IMyType<string>> createMockFunc = () =>
      {
      var mock = new Mock<IMyType<string>>();
      mock.Setup(m => m.GetMessage()).Returns("I'm a mocked IMyType<string>");
      return mock.Object;
      }

      var regBuilder = new RegistrationBuilder();

      // pseudo-code, how to do this in reality?
      regBuilder.ForType<IMyType<string>>().CreateInstance(createMockFunc);

      var catalog = new AggregateCatalog();
      var appCatalog = new ApplicationCatalog(regBuilder);
      catalog.Catalogs.Add(appCatalog);
      var container = new CompositionContainer(catalog);

      var factoryImporter = new FactoryImporter<string>();
      container.ComposeParts(factoryImporter);

      Assert.AreEqual(
      "I'm a mocked IMyType<string>",
      factoryImporter.MyTypeFactory.GetExport().Value.GetMessage());
      }
      }









      share|improve this question















      How can I mock a Managed Extensibility Framework (MEF) import into an ExportFactory[IMyType[T]], using mocks of IMyType[T] (being itself a generic interface)? Or, in general, specify instances to be returned by the ExportFactory, which can't be created with a constructor alone?



      Moq can't create a mock instance directly, as far as I know. I want an alternative to implementing the whole interface, which may be much larger and change, while I don't want to change the test.



      I once found a very complex ExportProvider code, with key-value strings, which didn't work if the generic type of the ExportFactory[T] was itself generic.



      // actual generic type is meaningless here, but it is a generic interface
      public interface IMyType<T>
      {
      string GetMessage();
      }

      public class FactoryImporter<T>
      {
      [Import]
      ExportFactory<IMyType<T>> MyTypeFactory {get;set;}
      }

      public class Tester
      {
      public void TestFactoryImporter()
      {
      Func<IMyType<string>> createMockFunc = () =>
      {
      var mock = new Mock<IMyType<string>>();
      mock.Setup(m => m.GetMessage()).Returns("I'm a mocked IMyType<string>");
      return mock.Object;
      }

      var regBuilder = new RegistrationBuilder();

      // pseudo-code, how to do this in reality?
      regBuilder.ForType<IMyType<string>>().CreateInstance(createMockFunc);

      var catalog = new AggregateCatalog();
      var appCatalog = new ApplicationCatalog(regBuilder);
      catalog.Catalogs.Add(appCatalog);
      var container = new CompositionContainer(catalog);

      var factoryImporter = new FactoryImporter<string>();
      container.ComposeParts(factoryImporter);

      Assert.AreEqual(
      "I'm a mocked IMyType<string>",
      factoryImporter.MyTypeFactory.GetExport().Value.GetMessage());
      }
      }






      c# .net moq mef






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 15:56

























      asked Nov 19 at 15:50









      Erik Hart

      567518




      567518





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378255%2fhow-to-mock-a-mef-exportfactorytgeneric-using-moq-instances%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378255%2fhow-to-mock-a-mef-exportfactorytgeneric-using-moq-instances%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'