开发者

using WebConfigurationManager in unit tests

I'm trying to write some unit tests that test code which alter web.config runtime. To load Web.config, WebConfigurationManager.OpenWebConfiguration is used. This does not work well in Unit tests.

What options do I have? I've tried:

  1. Configure the test as an ASP.NET-test with method attributes. This seems brittle because you need to hard code the port on which the development server runs with [UrlToTest("http://localhost:25153/WebSite1")] . I need the test to run on multiple computers configured differently. Also I feel it's overkill to launch an IIS server just to be able to load a text file in a unit test.

  2. Load the config from disk using开发者_高级运维 other methods. I've tried configurationManager.OpenExeConfiguration(), and I get a Configuration object back, but this doesn't seem to have been parsed correctly. Specifcially the sessionState section under system.web, which is a web-specific setting so that is not very surprising.

Where do I go from here?


What is your end goal in reading the web.config? Depending on what you are trying to accomplish, there are many different ways to read the web.config. If you are trying to read the connectionString use: Imports System.Web.Configuration.WebConfigurationManager Then in your code use: Dim cn As SqlConnection = New SqlConnection(ConnectionStrings("ConnectionStringName").ToString()) If you are trying to read custom app settings: Dim value as String = ConfigurationSettings.AppSettings("keynamehere") Just kind of depends on what you are trying to read..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜