Testing issue with RegionManager

Topics: Prism v2 - Silverlight 2, Prism v2 - Silverlight 3, Prism v2 - WPF 3.5
Mar 29, 2010 at 9:52 PM

Hi,

 

I have a intresting issue when I try to test my modules. What I need to do is test if a region has registered some views. For this I create use the region manager mock from the StockTrader application. The code for the test fixture is like this:

LocalUnityContainer = new MockUnityContainer();
LocalRegionManager = new MockRegionManager();
LocalRegionManager.Regions.Add(new MockRegion { Name = RegionNames.MainMenuRegion });

AdminModule = new AdminModule(LocalUnityContainer, LocalRegionManager);

AdminModule.Initialize();

The actual assert is a little complicated but it works:

 

Assert.True(((MockRegion)_regionManager.Regions[RegionNames.MainMenuRegion]).AddedViews
                .OfType<ModuleMenuItem>()
                .Where(v => v.TabName == TabNames.Administration && v.GroupName == GroupNames.AdministrationCommon && v.Button.Text == "Users")
                .SingleOrDefault() != null);

Now in the AdminModule Initialize function I have the following code:

_regionManager.RegisterViewWithRegion(RegionNames.MainMenuRegion, () => menuItem);

This will give the following exception:

 

System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current()
at Microsoft.Practices.Composite.Regions.RegionManagerExtensions.RegisterViewWithRegion(IRegionManager regionManagerString regionNameFunc`1 getContentDelegate)
If I replace it with: 
_regionManager.Regions[RegionNames.MainMenuRegion].Add(menuItem);
All works as expeced.
Any ideeas ?
Regards,

 

 

 

 

Developer
Mar 30, 2010 at 5:44 PM

Hi,

The call to the RegisterViewWithRegion method is apparently trying to get the current instance of ServiceLocator, but since it is null in the context of this test, a NullReferenceException is thrown. A possible approach could be mocking the ServiceLocator. You can find an example of this in the RegionManagerExtensionsFixture.cs file from the Composite.Tests project in the RI.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 30, 2010 at 6:18 PM

Hi Guido,

 

I figured out this myslef today also, but RegionManagerExtensionsFixture is used to test the class that contains the extension method, my purpose is a little diffrent and I can't see a way to use the code from there.

Any suggestions for a approach ?

 

Regards,

Apr 5, 2010 at 6:22 PM

Hi Calin,

Without knowing the specific code you are using it is not possible to provide a fully suitable solution for your scenario. As Guido explained, you need to find a way to mock the ServiceLocator so that the instance that is obtained from ServiceLocator.Current mocks the functionality you require. The mock provided in the CAL might not be usable as is, but is simply an example on one possible way to achieve this in a particular scenario.

A code sample that uses the ServiceLocator mock for a similar purpose to yours (using View Discovery to register views) can be found in the CreateModule method of the UIComposition.Modules.Employee.Desktop.Tests project inside the ViewDiscovery Quickstart. Again, it might not be re-usable in your tests, but it shows a possible approach to tackle this situation.

Please let me know if this helps.

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

Apr 5, 2010 at 7:45 PM

Hi Damian,

 

The ViewDiscovery tutorial is what I was looking for.

 

Thank you,