Unity Container access in NUnit test case

Topics: Prism v2 - WPF 3.5
Jul 27, 2009 at 2:29 PM


I need to write a NUnit test case for testing my following method in a Prism application.

How do I get a reference of Unity Container in my test class? Typically the NUnit test cases are built into a separate project from the main applicatioin.

My application registers a lot of services with the Unity container, Object Manager service is one example which needs to be tested

public override Object get(string guid)




                Object data = null;

                 ObjectManager objectManager = CompositeContainer.Container.Resolve<ObjectManager>();

                data = objectManager.Get<Object>(new Guid(guid));

                return data;


            catch (Exception ex)


                throw new InvalidOperationException(ex.Message);



Jul 27, 2009 at 6:57 PM


One possible approach I can think of is mocking the UnityContainer to suit your test needs. You can check out the MockUnityContainer class in the Mocks folder of the UnityExtension tests project for a possible implementation of the mock.

You can also check the Stock Trader RI tests to check out the different approaches the Prism team used to get the container in tests.

Please let me know if this helps.

Damian Schenkelman

Dec 1, 2009 at 10:56 AM

Hi  Damian Schenkelman,

I am just trying to do Nunit testing in the prism (SL) application. I am trying to implement the Mocks to write test cases with reference of Stock Trader RI, I have few Questions regarding Stock Trader RI application how did they implemented Mocks I did not find any explanation regarding testing.

Can i get some references to Implement Mocks with some Explanation.

Thanks in Advance.

Dec 1, 2009 at 8:03 PM


The Prism documentation doesn’t provide any particular information about how to create mock objects.

However if you want to dig deeper about mocking, you can take a look at the following articles:

I hope you can find this answer helpful.

Fernando Antivero
Dec 1, 2009 at 9:16 PM

Hi ,

Thanks a lot for reply I have one question.

I created a Sample Test Project and when i tried to run using Nunit I  am facing with this Error.

Error: Sample.UnitTesting.Tests.ConfigurationTest.TestConfiguration:
System.TypeLoadException : Method 'LoadBy' in type 'Sample.UnitTesting.Mocks.MockConfigurationService'
from assembly 'Sample.UnitTesting.Mocks,
Version=, Culture=neutral, PublicKeyToken=null' does not have an implementation.

I am getting same error even I write anything in [Test] method or left Empty. I have no idea any help is Appretiated.

Thanks in Advance