【发布时间】:2012-09-02 13:15:27
【问题描述】:
运行单元测试时,无法正确读取配置文件。 但是,当从控制台应用程序上下文运行时,会读取配置文件。
在下面的代码中,在赋值后访问var section时,运行测试TestIoCInit()时为null,从控制台应用程序调用InitIoC()时不为null .
[TestMethod]
public void TestIocInit()
{
InitIoC();
}
internal static void InitIoC()
{
IUnityContainer unityContainer = new UnityContainer(); // the host app domain creates the unity container and pass it to the resolver, the resolver is a static member of IoC class, thus the container is static
ServiceLocator.SetLocatorProvider(unityContainer);
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(unityContainer);
var unityResolver = new UnityDependencyResolver(unityContainer);
IoC.Initialize(unityResolver);
new IoCTypeRegistrationTask(unityContainer).Execute();
}
感谢您的帮助!
【问题讨论】:
标签: c# unit-testing app-config