Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Accessing web.config during unit tests

Reply
Thread Tools

Accessing web.config during unit tests

 
 
ChrisC
Guest
Posts: n/a
 
      01-27-2004
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!



----== 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 =---
 
Reply With Quote
 
 
 
 
Chris Jackson
Guest
Posts: n/a
 
      01-27-2004
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.

--
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" <(E-Mail Removed)-spam.invalid> wrote in message
news:4016b7fe$1_2@127.0.0.1...
>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!
>
>
>
> ----== 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
> =---



 
Reply With Quote
 
 
 
 
ChrisC
Guest
Posts: n/a
 
      01-28-2004
> 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 =---
 
Reply With Quote
 
Chris Jackson
Guest
Posts: n/a
 
      01-28-2004
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" <(E-Mail Removed)-spam.invalid> wrote in message
news:4017881e$1_2@127.0.0.1...
>> 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
> =---



 
Reply With Quote
 
ChrisC
Guest
Posts: n/a
 
      01-29-2004
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).



----== 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 =---
 
Reply With Quote
 
rochnet rochnet is offline
Junior Member
Join Date: Nov 2009
Posts: 1
 
      11-01-2009
i had same problem. So i decided to use a resource file instead of setting key data in the web.config file.

i moved all of my web.config data into a resource file, changed my code to lookup key data from the resource file instead of the web.config settings and was able to use csunit just fine.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
capturing stdout during unit tests Mark Slater Ruby 4 08-08-2007 09:08 PM
Junit tests, setting up tests without having to create a billion methods xyzzy12@hotmail.com Java 8 02-28-2006 08:59 PM
Tests without study guides or practice tests? =?Utf-8?B?Q2hyaXNS?= Microsoft Certification 8 12-20-2005 04:59 AM
Constant.t fails 240 of 272 tests and recurs.t fails 1 of 25 tests on HPUX using perl 5.8.7 dayo Perl Misc 11 12-16-2005 09:09 PM
"faking" an HTTPContext when running unit tests ASP .Net 3 02-05-2004 12:58 PM



Advertisments