Testing and Debugging MEF, Tips – Part 1

2010/07/09

Testing and Debugging MEF, Tips – Part 1

Text, Debug, MEF

this is the first post of a series that will offer some tips

about testing and debugging your MEF-able component and application.

 

in this post we will focus about exporting Mock objects.

 

the code sample for this post can be download from here.

 

Prerequisite

if you are not familiar with Mocks you better read more about this subject before

proceeding with this post (in short Mocks are fake object which is use for separate

dependencies during unit testing).

 

the sample is using the Moq library as it mocking infrastructure.

 

Having the following code:

1. contract

Contract
  1. public interface ILogger
  2. {
  3.     void Write(string message);
  4. }

 

2.exported contract implementation

Export (contract implementor)
  1. [Export (typeof(ILogger))]
  2. public class DebugLogger: ILogger
  3. {
  4.     public void Write(string message)
  5.     {
  6.         Debug.WriteLine(message);
  7.     }
  8. }

line1: export the class as ILogger

 

3. contract consumer

Import (contract consumer)
  1. public class MyLogic
  2. {
  3.     [ImportMany]
  4.     private IEnumerable<ILogger> Loggers { get; set; }
  5.  
  6.     public void DoYourThings()
  7.     {
  8.         foreach (ILogger logger in Loggers)
  9.         {
  10.             logger.Write("Some log");
  11.         }
  12.  
  13.         Console.WriteLine("Done");
  14.     }
  15. }

line 3: import any available loggers

 

4. composition and execution

Main
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         var logic = new MyLogic();
  6.         var cat = new AssemblyCatalog(typeof(Program).Assembly);
  7.         var container = new CompositionContainer(cat);
  8.         container.ComposeParts(logic);
  9.  
  10.         logic.DoYourThings();
  11.         logic.DoYourThings();
  12.  
  13.         Console.ReadKey();
  14.     }
  15. }

 

How can we write unit test for the DoYourThings method?

MEF dependencies is already loosely coupled by it nature, but in order to

validate the method behavior we still need to check whether the the method

do write logs.

we will inject a Mock<ILogger> into the MEF container in order to validate

this behavior.

 

the following code snippet is showing how:

Test
  1. [TestMethod]
  2. public void TestMethod1()
  3. {
  4.     // Arrange
  5.     var logic = new MyLogic();
  6.     var container = new CompositionContainer();
  7.     var loggerMock = new Mock<ILogger>();
  8.     container.ComposeExportedValue<ILogger>(loggerMock.Object);
  9.     container.ComposeParts(logic);
  10.  
  11.     // Test
  12.     logic.DoYourThings();
  13.  
  14.     // Validate
  15.     loggerMock.Verify(logger => logger.Write(It.IsAny<string>()), Times.Once());
  16. }

 

line 7: creating mock object.

line 8: inject the mock object into the container.

 

Summary

using the ComposeExportedValue method, we can inject

mock object into our CompositionContainer, therefore at the composition stage

the Import properties will get our mock objects.

 

kick it on DotNetKicks.com Shout it

Digg This
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>