Unity Container in Unit Testing code

Topics: Prism v1
Dec 19, 2009 at 12:53 AM

Hi there,

I'm using VS2008 Team System to perform unit tests. I want to test some units in MyService module, which provides services via an interface IMyService (the Separated Interface Pattern is used).  In my unit test code, I need to have the unity container to retrieve the service from MyService:

container.Resolve<IMyService>();

How can I obtain this unity container in the unit test code?

Thanks,

Jie

 

Dec 22, 2009 at 7:44 PM

Hi Jie,

As you are not testing the container, the approach that is usually followed is mocking the Unity Container and the functionality it provides. The following thread deals with a similar issue:

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Jan 5, 2010 at 7:29 PM

Hi Damian,

Thanks for your reply!

Here is what I did based on your suggestions and StockTraderRI:

  • Added Composite.UnityExtensions.Tests, which has a MockUnityContainer class.
  • In my code, I use "RegisterType<TFrom, TTo>() where TTo : TFrom" and "Resolver<T>()". I checked the MockUnityContainer class, and found that "Resolve<T>()" has been implemented, while the "RegisterType<>()" has not:

        public T Resolve<T>()
        {
            if (ResolveBag.ContainsKey(typeof(T)))
                return (T)ResolveBag[typeof(T)];

            throw new Exception();
        }

  • I tried to find the implementation for "RegiesterType<>()" in Composite.UnityExtensions, but the CAL does provide the source code for that.

So, here is my questions:

  1. My understanding is that as soon as I implement the "RegisterType<>()" method, I shoud be able to use the MockUnityContainer class for my unit tests (since the "Resolve" and "RegisterType" methods are the only ones I need for now). Am I right?
  2. How to implement "RegisterType<>()" method?
  3. Does Composite WPF provide all source code? If so, where can I find it (from what we got and use, the CAL only contains some of the code)?

Thanks,

Jie

Jan 6, 2010 at 4:35 PM

Hi Jie,

I will try to answer your questions separately.

  1. Mocks usually only implement the basic functionality that is required for them to work. If those methods are the only ones you need you should be good to go.
  2. The register type method is implemented with its non-generic signature in the MockUnityContainer class, so you can take the implementation from there. Instead of:
    IUnityContainer RegisterType<TFrom, TTo>(LifetimeManager lifetimeManager) where TTo : TFrom; 
    
    the one that is implemented is the following:
    IUnityContainer RegisterType(Type from, Type to, LifetimeManager lifetimeManager);
    
  3. The Composite Application Library comes with the CAL, Quickstarts and RI source code. You can find it in the Source folder where you extracted Prism. The specific library code that is provided by Prism is contained entirely in the CAL’s three projects:
    • Composite
    • Composite.Wpf
    • Composite.UnityExtensions

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Jan 6, 2010 at 9:40 PM

Hi Damian,

I did the following, as you suggested in 2:

public IUnityContainer RegisterType<TFrom, TTo>() where TTo : TFrom
        {
            return this.RegisterType(typeof(TFrom), typeof(TTo), (LifetimeManager) null);
        }

But it didn't work (got an exception when trying to call the Resolve method). By looking at the MockUnityContainer code, I found that RegisterType adds an item to Types, while Resolve tries to get it from ResolveBag, which is a different field in the MockUnityContainer class. Debugging results showed that the ResovleBag field was null (since nothing has been added to it), which caused the exception.

The CAL we got only has some of the source code. For example, it gives the IUnityContainer interface, but detailed implementation for RegisterType is not included. The three libraries you mentioned do provide source code, but the Unity library only has its dll without source code.

Meanwhile, I downloaded Unity 1.0 and 1.2 source code from codeplex. However, their implementation looks different from the CAL for the UnityContainer class, which I thought was used for Composite WPF.

Any further suggestions?

Thanks,

Jie

 

 

Jan 13, 2010 at 1:07 AM

Hi Damian,

Please disregard my questions. I got the issue resolved.

Thanks,

Jie