Unit tests with MEF2 ExportFactories

We recently switched to using .Net4.5, and I'm doing some refactoring to take advantage of the new ExportFactories.

My question is, how do I mock them for unit testing when I insert them into my constructor, but I'm not 100% sure of the best approach to them in terms of testing.

+4
source share
1 answer

The ExportFactory constructor accepts a function that returns a tuple containing the export, and another function that frees the export. So you can do something like this:

static Tuple<ISomething, Action> CreateMock()
{        
    return new Tuple<ISomething, Action>(new MockSomething(), 
                                         () => Console.WriteLine("Releasing..."));
}

and add it to the constructor of another class with something like:

var obj = new OtherClass(new ExportFactory<ISomething>(CreateMock));
+2
source

All Articles