Unity in unittesting project

Aug 16, 2012 at 9:29 AM

Is it possible to use a unity.config file in a UnitTest project?

I have some existing code which uses a lot of IoC, now I like to do some more testing the code. Therefore i've setup some test-projects and now I have to configure Unity in the testproject.
For some reason, I can't use Unity in the testproject, because the Unity.config file doesn't seems to get loaded.

Can some point me the right direction??

Aug 16, 2012 at 4:07 PM

Yes, you can use a Unity.config file in a test project.  Although in a test project you might want to configure the container programmatically because it allows you to more easily recreate various scenarios.

If you want to use a config file you can place your unity configuration in app.config for the test project or you can manually load a configuration file from another location:

var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = fileName};
System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

var unitySection = configuration.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
var container = new UnityContainer().LoadConfiguration(unitySection);

If you are using Visual Studio's unit testing then you could also use the deployment functionality to deploy the files you need and you could then load them:

[TestClass]
public class CacheTestFixture
{
    private IUnityContainer container;

//        Also could initialize one container for the entire test
//        [Microsoft.VisualStudio.TestTools.UnitTesting.TestInitialize]
    public void SetUp(string fileName)
    {
        var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = fileName};
        System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        var unitySection = configuration.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
        var container = new UnityContainer().LoadConfiguration(unitySection);
    }

    [TestMethod]
    [DeploymentItem("C:\\Config\\Unity.Config")]
    public void RetrieveCachedValueThroughSameInstance()
    {
        SetUp("Unity.config");
        var empBiz = container.Resolve<IEmployeeBiz>();

        string a = empBiz.ListAllInSet();
        string c = empBiz.ListAllInSetFacade();

        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(a, c);
    }

    [TestMethod]
    [DeploymentItem("C:\\Config\\Unity_bad.Config")]
    [ExpectedException(typeof(InvalidOperationException))]
    public void RetrieveCachedValueInvalidConfigType()
    {
        SetUp("Unity_bad.config");
        var empBiz = container.Resolve<IEmployeeBiz>();

        string a = empBiz.ListAllInSet();
        string c = empBiz.ListAllInSetFacade();

        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(a, c);
    }
}

Although usually you would have only one config for the entire test.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 17, 2012 at 11:34 AM

Randy,

thanks for your reply. It helped me a lot in setting up the test-project using unity.


Unfortunatly it brought an other problem:
In the testproject I have a app.config which refers to the Unity.config file.
If I try to load the Unity.Config file: "[DeploymentItem("/Unity.Config")]" I get the error: "Configuration file Unity.Config does not have root <configuration> tag (C:....". I understand this exception, and what caused it.
In the App.Config I use: "
<configSections>
 <
section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</
configSections>

<unity configSource="Unity.config" />"

So the App.Config is loading the Unity.Config on 'demand'.

How can I let the Testproject copy the Unity.Config file with the App.Config file to the /Out - folder?

 

Aug 19, 2012 at 1:52 AM
Edited Aug 19, 2012 at 3:23 PM

If you are using a configSource, then you could just add an app.config to your test project (app.config will automatically be deployed to the output folder):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity configSource="Unity.config" />

</configuration>

Then specify Unity.config as a deployment item (this will deploy the file to the output folder) and create a container in the test method:

    [TestMethod]
    [DeploymentItem("C:\\EntLib\\Config\\Unity.Config")]
    public void RetrieveCachedValueThroughSameInstance()
    {
        var container = new UnityContainer().LoadConfiguration();
        var empBiz = container.Resolve<IEmployeeBiz>();

        string a = empBiz.ListAllInSet();
        string c = empBiz.ListAllInSetFacade();

        Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(a, c);
    }

 

Alternatively, you could add the Unity.Config as well as the app.config files to the Test Settings->Deployment, files list and they both should be copied to the output folder.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 20, 2012 at 8:42 AM

I monitored the output and found that the app.config and unity.config file are both copied. But when I start testing, I get an error: Unity.config-file is not found in c:\<user>\...\AppData\Temp\.. So the testproject is not looking in the output folder, but in the .Net runtime folder. How can i change this?

Aug 21, 2012 at 1:28 AM

I would think that DeploymentItemAttribute would have copied to the directory where the tests will execute.  I'm curious if, in  Local.testsettings, the "Enable Deployment" is checked?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 22, 2012 at 10:38 AM

The "Enable Deployment" is checked! Thats also the location where i've set the unity.config file to deploy.
The testproject only referers to the .NET folder to lookup for the unity.config file instead of the testproject Out folder.

Is there an option to set the execution path for the testproject? (The testproject runs fine, but maybe it helps to locate the unity.config file)

For now i've pasted the contents of the Unity.config file in the Unity-section in the app.config file, but i'd hoped i can use the Unity.config file itself.

Aug 23, 2012 at 4:40 AM

Everything works fine on my end so it's probably some settings, configuration, or environment.  Can you create a small sample so I can see the behavior you are seeing?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com