services not being added while unit testing

Topics: CAB & Smart Client Software Factory
Jan 14, 2006 at 6:13 AM
originally posted by: Paz33

Background: We have a SOA system. Prior to running the app, we need to get info that will be used throughout the system. In the main shell assembly, we have some services (designated using the service attributes for those service classes). In the ShellApplication class, we are hydrating the services in the AfterShellCreated override. We are using the unit test environment in VS2005.

In the test class, I am calling the ShellApplication.Run() in the TestInitialization method to ensure that the CAB code to create the services are being executed. I am trying to validate that the services are being hydrated in the AfterShellCreated_Test() method. The problem I am having is that when I run unit tests, the CAB is not creating my services. Running it in regular mode, the CAB creates the services just fine. In debugging, I found where the problem is coming from but I am not sure how to resolve it.

What I found was that in the CabApplication.cs file, when it is trying to perform reflection on the assembly in unit test mode, the Assembly.GetEntryAssembly() method is returning a null. Because of this, reflection is not being done on the assembly containing the service attribute tags. Once again, this works fine in the regular mode, just not in the test mode.

private void ProcessShellAssembly()
IModuleLoaderService loader = rootWorkItem.Services.Get<IModuleLoaderService>(true);
Assembly assembly = Assembly.GetEntryAssembly();

if (assembly != null)
loader.Load(rootWorkItem, assembly);

I even tried to use Assembly.GetCallingAssembly() but that did not work either. When doing this, I got the test assembly (which does not contain the service classes) and not the main assembly.

Any help or suggestions is greatly appreciated.
Jan 14, 2006 at 6:50 AM
originally posted by: BradWilsonMSFT

The CabApplication class is not designed to be unit tested (we tried to make it happen, but it's just too fundamentally tied to the startup of an actual Windows Forms application that it's not appropriate for testing).

The easiest thing to do is extract any code from your application class that needs to be tested, and test that instead. Then you'll have one line of untested code: your application class calling your tested object. You can trust that that one line of code works without testing it.
Jan 14, 2006 at 7:20 AM
originally posted by: Paz33

Thanks for your timely response.

The main purpose of overriding the AfterShellCreated method is to hydrate the services that should have been created within the CAB. My unit test is to get the service from the RootWorkItem and check to see if it is hydrated.

I will revise my test since I can't test if the services are hydrated. I will try your suggestion and extract the code.