Accessing web.config during unit tests

  • Thread starter Thread starter ChrisC
  • Start date Start date
C

ChrisC

I am a relative newbie to .NET - two weeks, using everyday. My problem
is this. I have my asp.net application where the web.config contains
the database connection string. Normally i access this fine with
Application[ "connectionString" ].

However, I am now trying to use a unit testing framework (csunit
1.9.2) and when I try and run the tests I have created it is saying
"The ConnectionString property has not been initialized". The piece
of code it is using runs fine if I access it by clicking a button
that triggers it in one of my aspx pages, but not when it is
initiated by the unit test class.

Potential troubles - the class I am running the method in is a
standalone utility class ie it doesnt have a aspx page associated.
Dont know if this matters or not....


public string getXmlCommandByName( string commandName )
{
SqlConnection conn;
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )
{
conn.Open();

string sqlString = "SELECT xml FROM PDSXML WHERE command =
" + commandName;
SqlCommand request = new SqlCommand( sqlString, conn );
SqlDataReader results =
request.ExecuteReader();

return results.GetValue( 0 ).ToString();
}
}

As you can see i have been trying using different ways of accessing
the web.config

Help!
 
If you are storing values in web.config, you access them using
ConfigurationSettings.AppSettings, which will pull values from the
appSettings portion of the web.config.
 
My bad - I just noticed that in the top text, where you were looking at
Application.

If you are not actively inside of a web application, you won't find the
web.config. However, you can put an app.config in that directory and put
sections in there, which is what you would do for a WinForms app. I haven't
tried this exact scenario specifically, but that's why it isn't working, and
what you should be able to do in order to fix things up. Sorry I wasn't
reading closely enough before. :-S



--
Chris Jackson
Software Engineer
Microsoft MVP - Windows Client
Windows XP Associate Expert
--
More people read the newsgroups than read my email.
Reply to the newsgroup for a faster response.
(Control-G using Outlook Express)
--

ChrisC said:
using (conn = new SqlConnection(
ConfigurationSettings.AppSettings[ "connectionString"
] ) )

Like that? :) (from the code i quoted)



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000
Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
No worries. It seems to be a bit of a pain this. Its a web application
and i dont want to create a effectively duplicate config file just
for the tests as that would be silly as it wouldnt be using the
actual config settings used by the application. I tried writing a
class to access the web.config directly but it wasnt having any of it
- in use by another process (i assume the web app keeps it constantly
open).
 
Back
Top