Problem in reading connection string from App.Config when using NUnit 2.5.2

醉酒当歌 提交于 2019-12-17 13:06:33

问题


I'm using Microsoft Visual Studio 2005 with Enterprise Library 3.1.

I have a data access layer which is a separate visual studio class library project. I wrote unit tests in a another class library and trying to call the data access method, but I keep getting

PSMCP.Dal.Tests.DataManagerTests.GetAAAReturnsDataReader:
System.NullReferenceException : Object reference not set to an instance of an object.

at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseMapper.MapName(String name, IConfigurationSource configSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp[TTypeToBuild](IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IReadWriteLocator locator, IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp[T](IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.NameTypeFactoryBase`1.CreateDefault()
at Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase()
at PSMCP.Dal.PSMCPDataManager.GetAAACall(String searchStr, Int32 filterCategory, Int32 centerId) in C:\Documents and Settings\user1\My Documents\Visual Studio 2005\Projects\Test\AAA.Dal\DataManager.cs:line 61
at PSMCP.Dal.Tests.DataManagerTests.GetAAAReturnsDataReader() in C:\Documents and Settings\user1\My Documents\Visual Studio 2005\Projects\Test\PSMCP.Dal.Tests\DataManagerTests.cs:line 27

I guess the DatabaseFactory.CreateDatabase() call is not able to read the App.Config to get the connection string. The Unit Test project has a valid App.Config which contains the connection string settings [added by EntLib config utility]. I created a console application and added the same App.Config and it runs fine from Console application. I'm confused why it would not run when running from NUnit GUI runner.

Any ideas?

Thanks, Matrix M.


回答1:


You need to tell nunit the config file name. in nunit gui go to project > edit and then change the configuration file name.

It seems nunit looks for namespace.config by default. (eg. tb.specs.config in my case)




回答2:


The issue is that NUnit look for config files in the root directory (the design-time location of config files). However, .net moves and renames the files into the bin folder. You can change the nunit settings to look in the bin when executing tests.



来源:https://stackoverflow.com/questions/1633607/problem-in-reading-connection-string-from-app-config-when-using-nunit-2-5-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!